/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Design.java
 *
 * Created on Oct 16, 2012, 8:25:51 PM
 */
package UserInterface;

import GestureRecognition.Slide;
import java.awt.AWTException;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import static com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import controller.ColorMeanController;
import controller.SkinBasedController;
import coordinates.ArrayFilter;
import insurface.InteractiveSurface;
import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.UIManager;
import setting.ButtonCalib;
import test.shashindra.Kalman;
import test.shashindra.ShashKalmanTest;
import utilities.Calibrate7;
import utilities.CalibrateTouch;
import utilities.CoordinateFinder;
import utilities.KalmanPoints;
import utilities.PlayMusic;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;

/**
 *
 * @author HOME
 */
public class ProjectorGUINew extends InteractiveSurface {

    /** Creates new form Design */
    private static int saveImageNum = 0;
    private ImageIcon ic1 = new ImageIcon();
    private ImageIcon ic2 = new ImageIcon();
    private ImageIcon ic3 = new ImageIcon();
    private ImageIcon ic4 = new ImageIcon();
    private ImageIcon drawing = new ImageIcon();
    static boolean isImageSet = false;
    private CvSize imSize = new CvSize(140, 130);
    private IplImage tempIm = cvCreateImage(imSize, 8, 3);
    private IplImage tempSkin = cvCreateImage(imSize, 8, 1);
    private IplImage tempIm2 = cvCreateImage(imSize, 8, 3);
    private IplImage tempSkin2 = cvCreateImage(imSize, 8, 1);
    private static int ii = 0;
    private int prevX = 0, prevY = 0;
    static Timer initTimer;
    Timer timer;
    Thread thread;
    Color linecolor = Color.BLACK;
    private boolean viewCamPressed = false;
    private boolean deviceStarted = false, clearSet = false, isFingerSet = true, isEraserPressed = false;
    Graphics2D grap;
    JFileChooser fc = new JFileChooser();
    File file;
    BufferedImage bf = null, saveImage = null;
    BufferedImage[] imageDb = new BufferedImage[5];
    BufferedImage[] iconDb = new BufferedImage[5];
    private int ovalSize = 5;
    Robot rb;
    int imWidth = 1000;
    int imHeight = 657;
    int lineWidth=3;
    ////////////////////////////////////////////////////////////////////////////////////////
    boolean isCalibrating = false, calibration2 = false, preCalibrated;
    boolean isCal1, isCal2, isCal3, isCal4, isCal5, isCal6, isCal7;
    boolean finished1, finished2, finished3, finished4, finished5, finished6, finished7;
    int minX = 4000, maxX = 0, count = 0;
    int totX, totY, minY = 1000, maxY = 0;
    int x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6, x7, y7;
    double a = 1, b, c, d, e = 1, f, g, h, i, j;//used for touchpad calibration process
    double aw, bw, cw = 1, dw, ew, fw, gw, hw, iw = 1, jw;
    int state = 0;
    int[] xVals = new int[30], yVals = new int[30];
    Slide sl = new Slide();
    int[] coordinate = new int[2];
    private int frameNo = 0;
    private int frameCount = 15;
    int[][] pointDb = new int[frameCount][2];
    private int gestureType = 0;
    KalmanPoints kalman = new KalmanPoints();
    boolean findingLimit = false;
    private int[] drawlabelco = new int[2];
    private int[] jmenuco = new int[2];
    private int[] startbuttonco = new int[2];
    private int[] stopbuttonco = new int[2];
    private int[] pausebuttonco = new int[2];
    private int[] camerabuttonco = new int[2];
    private int[] bluebuttonco = new int[2];
    private int[] redbuttonco = new int[2];
    private int[] eraserbutonco = new int[2];
    private int[] clearbuttonco = new int[2];
    private int[] picbuttonco = new int[2];
    private int[] nextbuttonco = new int[2];
    private int[] backbuttonco = new int[2];
    private int[] blackbuttonco = new int[2];
    int x_offset = 0;
    int y_offset = 0;
    int Bstate = 0;
    int prevBstate = 0;
    int Bcount = 0;
    File directory = null;
    int srcLength = 0;
    int srcHeight = 0;
    //////////////////////////////////////////////////////////////////////////////////////////////
    boolean calibrateButton = false;
    int buttonStatus = 0;
    int butCount = 0;
    int[] butX = new int[30], butY = new int[30];
    int[] midBut = new int[9];
    int minXBut, maxXBut;
    private boolean stylusChanged = false;
    private double maxPointSeperation = 100;
    PlayMusic pm = new PlayMusic();
    String bstateName = null;
    public boolean Kalman = false;

    public void setIsFingerSet(boolean isFingerSet) {
        this.isFingerSet = isFingerSet;
    }

    public void loadPre() {
        a = settings.getA();
        b = settings.getB();
        c = settings.getC();
        d = settings.getD();
        e = settings.getE();
        f = settings.getF();
        g = settings.getG();
        h = settings.getH();
        i = settings.getI();
        j = settings.getJ();
        preCalibrated = true;
    }

    private boolean inRange1(int x, int y) {
        if (250 < x && x < 550 && 0 < y && y < 400) {
            return true;
        } else {
            return false;
        }
    }

    private boolean inRange2(int x, int y) {
        if (0 < x && x < 200 && 500 < y && y < 1000) {
            return true;
        } else {
            return false;
        }
    }

    private boolean inRange3(int x, int y) {
        if (550 < x && x < 1000 && 500 < y && y < 1000) {
            return true;
        } else {
            return false;
        }
    }

    private boolean range1(int x, int y) {
        if (100 < x && x < 425 && 200 < y && y < 500) {
            return true;
        } else {
            return false;
        }
    }

    private boolean range2(int x, int y) {
        if (575 < x && x < 1000 && 200 < y && y < 500) {
            return true;
        } else {
            return false;
        }
    }

    private boolean range3(int x, int y) {
        if (100 < x && x < 325 && 550 < y && y < 1000) {
            return true;
        } else {
            return false;
        }
    }

    private boolean range4(int x, int y) {
        if (375 < x && x < 500 && 550 < y && y < 1000) {
            return true;
        } else {
            return false;
        }
    }

    private boolean range5(int x, int y) {
        if (575 < x && x < 800 && 550 < y && y < 1000) {
            return true;
        } else {
            return false;
        }
    }
    //////////////////////////////////////////////////////////////////////////////////////////////

    public ProjectorGUINew() {
        initComponents();
        //System.out.println(startButton.getWidth()+","+startButton.getHeight());
        //   PBar.setVisible(true
        prevX = CoordinateFinder.getLENGTH();
        prevY = CoordinateFinder.getHEIGHT();
        directory = new File("Images");

        imWidth = drawLabel.getWidth() - 30;
        imHeight = drawLabel.getHeight() - 30;

        //  System.out.println("width= "+imWidth+" Height= "+imHeight);

        saveImage = new BufferedImage(imWidth, imHeight, BufferedImage.TYPE_INT_ARGB);

        grap = (Graphics2D) saveImage.getGraphics();
        try {
            rb = new Robot();
        } catch (AWTException ex) {
            Logger.getLogger(ProjectorGUINew.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (int i = 0; i < iconDb.length; i++) {
            try {
                String iconName = "src/images/" + (i + 1) + ".png";
                iconDb[i] = ImageIO.read(new File(iconName));
            } catch (IOException ex) {
                Logger.getLogger(ProjectorGUINew.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        loadButtonCalib();
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {

                int option = JOptionPane.showConfirmDialog(rootPane, "Do you want to save the calibration settings before exiting?", "EXIT", JOptionPane.YES_NO_CANCEL_OPTION);
                if (option == JOptionPane.YES_OPTION) {
                    JFileChooser fch = new JFileChooser();
                    fch.showSaveDialog(rootPane);
                    File file1 = fch.getSelectedFile();
                    {
                        ObjectOutputStream oos = null;
                        try {
                            FileOutputStream fos = new FileOutputStream(file1);
                            oos = new ObjectOutputStream(fos);
                            oos.writeObject(getSettings());
                            oos.close();
                            fos.close();

                            oos.close();
                            saveSettings();
                            System.exit(0);
                        } catch (IOException ex) {
                            //do nothing

                            Logger.getLogger(ProjectorGUINew.class.getName()).log(Level.SEVERE, null, ex);
                        } finally {
                        }
                    }
                } else {
                    saveSettings();
                    System.exit(0);
                }


            }
        });
    }

    public void loadButtonCalib() {
        FileInputStream fileIn = null;
        try {
            ButtonCalib jk;
            fileIn = new FileInputStream("buttonSet.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            jk = (ButtonCalib) in.readObject();
            in.close();
            fileIn.close();
            buttonMidX = jk.xVal;
            buttonMidY = jk.yVal;
            calMidBut();

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(InitialScreen.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(InitialScreen.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fileIn.close();
            } catch (IOException ex) {
                Logger.getLogger(InitialScreen.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void saveSettings() {
        try {
            FileOutputStream fileOut = new FileOutputStream("FinalSettings.ser");
            System.out.println("saved");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(getSettings());
            out.close();
            fileOut.close();
        } catch (IOException ex) {
            Logger.getLogger(ProjectorGUINew.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        displayPanel = new javax.swing.JPanel();
        leftLiveCamLabel = new javax.swing.JLabel();
        leftSkinCamLabel = new javax.swing.JLabel();
        rightLiveCamLabel = new javax.swing.JLabel();
        rightSkinCamLabel = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        startButton = new javax.swing.JButton();
        pauseButton = new javax.swing.JButton();
        stopButton = new javax.swing.JButton();
        cameraButton = new javax.swing.JButton();
        jToolBar2 = new javax.swing.JToolBar();
        blackPenButton = new javax.swing.JButton();
        redPenButton = new javax.swing.JButton();
        bluePenButton = new javax.swing.JButton();
        eraserButton = new javax.swing.JButton();
        clearScreen = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        jToolBar3 = new javax.swing.JToolBar();
        nextButton = new javax.swing.JButton();
        prevButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        statusLabel = new javax.swing.JLabel();
        drawLabel = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        deviceMenu1 = new javax.swing.JMenuItem();
        stopMenu = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        fingerMenu = new javax.swing.JMenuItem();
        redStylusMenu = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        rightCamMenu = new javax.swing.JMenuItem();
        toolMenu = new javax.swing.JMenu();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();
        saveMenu = new javax.swing.JMenuItem();
        insertImageMenu = new javax.swing.JMenuItem();
        lineWidthMenu = new javax.swing.JMenuItem();
        changeStylus = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        nextPageMenu = new javax.swing.JMenuItem();
        prevPageMenu = new javax.swing.JMenuItem();
        jMenu5 = new javax.swing.JMenu();
        directoryMenu = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu7 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem11 = new javax.swing.JMenuItem();
        jMenuItem12 = new javax.swing.JMenuItem();
        jMenuItem13 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem14 = new javax.swing.JMenuItem();
        setMaxPointSep = new javax.swing.JMenuItem();
        applyKalman = new javax.swing.JMenuItem();
        setWindow = new javax.swing.JMenuItem();
        jMenu6 = new javax.swing.JMenu();
        productHelpMenu = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Interactive Surface");
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setExtendedState(MAXIMIZED_BOTH);

        displayPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Display", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        leftLiveCamLabel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Left Live Cam"));

        leftSkinCamLabel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Left Skin Image"));

        rightLiveCamLabel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Right Live Cam"));

        rightSkinCamLabel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Right Skin Image"));

        javax.swing.GroupLayout displayPanelLayout = new javax.swing.GroupLayout(displayPanel);
        displayPanel.setLayout(displayPanelLayout);
        displayPanelLayout.setHorizontalGroup(
            displayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(leftLiveCamLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
            .addComponent(leftSkinCamLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
            .addComponent(rightLiveCamLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
            .addComponent(rightSkinCamLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        displayPanelLayout.setVerticalGroup(
            displayPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(displayPanelLayout.createSequentialGroup()
                .addComponent(leftLiveCamLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(leftSkinCamLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rightLiveCamLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rightSkinCamLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jToolBar1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jToolBar1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar1.setToolTipText("");

        startButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Play.png"))); // NOI18N
        startButton.setToolTipText("Start Device");
        startButton.setFocusable(false);
        startButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        startButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(startButton);

        pauseButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/player-pause.png"))); // NOI18N
        pauseButton.setFocusable(false);
        pauseButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        pauseButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        pauseButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                pauseButtonMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                pauseButtonMouseExited(evt);
            }
        });
        pauseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pauseButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(pauseButton);

        stopButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Stop playing.png"))); // NOI18N
        stopButton.setToolTipText("Stop Device");
        stopButton.setFocusable(false);
        stopButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        stopButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        stopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(stopButton);

        cameraButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Camera.png"))); // NOI18N
        cameraButton.setToolTipText("Show Cameras");
        cameraButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        cameraButton.setOpaque(false);
        cameraButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        cameraButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                cameraButtonMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                cameraButtonMouseExited(evt);
            }
        });
        cameraButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cameraButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(cameraButton);
        cameraButton.getAccessibleContext().setAccessibleParent(this);

        jToolBar2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jToolBar2.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar2.setRollover(true);

        blackPenButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Pen-icon.png"))); // NOI18N
        blackPenButton.setToolTipText("Pen");
        blackPenButton.setFocusable(false);
        blackPenButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        blackPenButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        blackPenButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                blackPenButtonMouseEntered(evt);
            }
        });
        blackPenButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                blackPenButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(blackPenButton);

        redPenButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Pen-Red-icon.png"))); // NOI18N
        redPenButton.setFocusable(false);
        redPenButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        redPenButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        redPenButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                redPenButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(redPenButton);
        redPenButton.getAccessibleContext().setAccessibleParent(this);

        bluePenButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Pen-Blue-icon.png"))); // NOI18N
        bluePenButton.setFocusable(false);
        bluePenButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bluePenButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bluePenButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bluePenButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(bluePenButton);

        eraserButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Eraser.png"))); // NOI18N
        eraserButton.setToolTipText("Eraser");
        eraserButton.setFocusable(false);
        eraserButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        eraserButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        eraserButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eraserButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(eraserButton);

        clearScreen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Clear.png"))); // NOI18N
        clearScreen.setToolTipText("Clear Screen");
        clearScreen.setFocusable(false);
        clearScreen.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        clearScreen.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        clearScreen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearScreenActionPerformed(evt);
            }
        });
        jToolBar2.add(clearScreen);

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Save as.png"))); // NOI18N
        saveButton.setFocusable(false);
        saveButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        saveButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(saveButton);

        jToolBar3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jToolBar3.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar3.setRollover(true);

        nextButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Forward.png"))); // NOI18N
        nextButton.setFocusable(false);
        nextButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        nextButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });
        jToolBar3.add(nextButton);

        prevButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Back.png"))); // NOI18N
        prevButton.setFocusable(false);
        prevButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        prevButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        prevButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                prevButtonActionPerformed(evt);
            }
        });
        jToolBar3.add(prevButton);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/1.png"))); // NOI18N
        jButton1.setToolTipText("Page 1");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar3.add(jButton1);

        statusLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        statusLabel.setText("Status: ");
        statusLabel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        drawLabel.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Screen", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        jMenu1.setText("Device");
        jMenu1.setFont(new java.awt.Font("Arial", 1, 12));

        deviceMenu1.setFont(new java.awt.Font("Arial", 1, 12));
        deviceMenu1.setText("Start Device");
        deviceMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deviceMenu1ActionPerformed(evt);
            }
        });
        jMenu1.add(deviceMenu1);

        stopMenu.setFont(new java.awt.Font("Arial", 1, 12));
        stopMenu.setText("Stop Device");
        stopMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopMenuActionPerformed(evt);
            }
        });
        jMenu1.add(stopMenu);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Stylus");
        jMenu2.setFont(new java.awt.Font("Arial", 1, 12));

        fingerMenu.setFont(new java.awt.Font("Arial", 1, 12));
        fingerMenu.setText("Finger");
        fingerMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fingerMenuActionPerformed(evt);
            }
        });
        jMenu2.add(fingerMenu);

        redStylusMenu.setFont(new java.awt.Font("Arial", 1, 12));
        redStylusMenu.setText("Red Stylus");
        redStylusMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                redStylusMenuActionPerformed(evt);
            }
        });
        jMenu2.add(redStylusMenu);

        jMenuBar1.add(jMenu2);

        jMenu3.setText("Display");
        jMenu3.setFont(new java.awt.Font("Arial", 1, 12));

        rightCamMenu.setFont(new java.awt.Font("Arial", 1, 12));
        rightCamMenu.setText("Start Cameras");
        rightCamMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rightCamMenuActionPerformed(evt);
            }
        });
        jMenu3.add(rightCamMenu);

        jMenuBar1.add(jMenu3);

        toolMenu.setText("Tools");
        toolMenu.setFont(new java.awt.Font("Arial", 1, 12));
        toolMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toolMenuActionPerformed(evt);
            }
        });

        jMenuItem9.setFont(new java.awt.Font("Arial", 1, 12));
        jMenuItem9.setText("Eraser");
        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem9ActionPerformed(evt);
            }
        });
        toolMenu.add(jMenuItem9);

        jMenuItem10.setFont(new java.awt.Font("Arial", 1, 12));
        jMenuItem10.setText("Clear Screen");
        toolMenu.add(jMenuItem10);

        saveMenu.setFont(new java.awt.Font("Arial", 1, 12));
        saveMenu.setText("Save Images");
        saveMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveMenuActionPerformed(evt);
            }
        });
        toolMenu.add(saveMenu);

        insertImageMenu.setFont(new java.awt.Font("Arial", 1, 12));
        insertImageMenu.setText("Insert Background Image");
        insertImageMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertImageMenuActionPerformed(evt);
            }
        });
        toolMenu.add(insertImageMenu);

        lineWidthMenu.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        lineWidthMenu.setText("Line Width");
        lineWidthMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lineWidthMenuActionPerformed(evt);
            }
        });
        toolMenu.add(lineWidthMenu);

        changeStylus.setFont(new java.awt.Font("Arial", 1, 12));
        changeStylus.setText("Change Stylus");
        changeStylus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changeStylusActionPerformed(evt);
            }
        });
        toolMenu.add(changeStylus);

        jMenuBar1.add(toolMenu);

        jMenu4.setText("Go to");
        jMenu4.setFont(new java.awt.Font("Arial", 1, 12));
        jMenu4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu4ActionPerformed(evt);
            }
        });

        nextPageMenu.setFont(new java.awt.Font("Arial", 1, 12));
        nextPageMenu.setText("Next Page");
        nextPageMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextPageMenuActionPerformed(evt);
            }
        });
        jMenu4.add(nextPageMenu);

        prevPageMenu.setFont(new java.awt.Font("Arial", 1, 12));
        prevPageMenu.setText("Previous Page");
        prevPageMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                prevPageMenuActionPerformed(evt);
            }
        });
        jMenu4.add(prevPageMenu);

        jMenuBar1.add(jMenu4);

        jMenu5.setText("Settings");
        jMenu5.setFont(new java.awt.Font("Arial", 1, 12));

        directoryMenu.setFont(new java.awt.Font("Arial", 1, 12));
        directoryMenu.setText("Working Directory");
        directoryMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                directoryMenuActionPerformed(evt);
            }
        });
        jMenu5.add(directoryMenu);

        jMenuItem1.setFont(new java.awt.Font("Arial", 1, 12));
        jMenuItem1.setText("Save Settings");
        jMenu5.add(jMenuItem1);

        jMenuItem2.setFont(new java.awt.Font("Arial", 1, 12));
        jMenuItem2.setText("Callibrate Buttons");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem2);

        jMenuItem3.setFont(new java.awt.Font("Arial", 1, 12));
        jMenuItem3.setText("Load Button Calibration");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem3);

        jMenu7.setText("Calibrate each button");
        jMenu7.setFont(new java.awt.Font("Arial", 1, 12));

        jMenuItem4.setFont(new java.awt.Font("Arial", 1, 12));
        jMenuItem4.setText("Black Pen");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu7.add(jMenuItem4);

        jMenuItem5.setFont(new java.awt.Font("Arial", 1, 12));
        jMenuItem5.setText("Red Pen");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu7.add(jMenuItem5);

        jMenuItem7.setFont(new java.awt.Font("Arial", 1, 12));
        jMenuItem7.setText("Blue Pen");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu7.add(jMenuItem7);

        jMenuItem8.setFont(new java.awt.Font("Arial", 1, 12));
        jMenuItem8.setText("Eraser");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu7.add(jMenuItem8);

        jMenuItem11.setFont(new java.awt.Font("Arial", 1, 12));
        jMenuItem11.setText("Clear");
        jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem11ActionPerformed(evt);
            }
        });
        jMenu7.add(jMenuItem11);

        jMenuItem12.setFont(new java.awt.Font("Arial", 1, 12));
        jMenuItem12.setText("Save");
        jMenuItem12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem12ActionPerformed(evt);
            }
        });
        jMenu7.add(jMenuItem12);

        jMenuItem13.setFont(new java.awt.Font("Arial", 1, 12));
        jMenuItem13.setText("Next");
        jMenuItem13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem13ActionPerformed(evt);
            }
        });
        jMenu7.add(jMenuItem13);

        jMenuItem6.setFont(new java.awt.Font("Arial", 1, 12));
        jMenuItem6.setText("Previous");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu7.add(jMenuItem6);

        jMenuItem14.setFont(new java.awt.Font("Arial", 1, 12));
        jMenuItem14.setText("Calculate");
        jMenuItem14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem14ActionPerformed(evt);
            }
        });
        jMenu7.add(jMenuItem14);

        jMenu5.add(jMenu7);

        setMaxPointSep.setFont(new java.awt.Font("Arial", 1, 12));
        setMaxPointSep.setText("Set Maximum point seperation");
        setMaxPointSep.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setMaxPointSepActionPerformed(evt);
            }
        });
        jMenu5.add(setMaxPointSep);

        applyKalman.setFont(new java.awt.Font("Arial", 1, 12));
        applyKalman.setText("Apply Kalman Filter");
        applyKalman.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                applyKalmanActionPerformed(evt);
            }
        });
        jMenu5.add(applyKalman);

        setWindow.setFont(new java.awt.Font("Arial", 1, 12));
        setWindow.setText("Set Window Size For Smotthing");
        setWindow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setWindowActionPerformed(evt);
            }
        });
        jMenu5.add(setWindow);

        jMenuBar1.add(jMenu5);

        jMenu6.setText("Help");
        jMenu6.setFont(new java.awt.Font("Arial", 1, 12));
        jMenu6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu6ActionPerformed(evt);
            }
        });

        productHelpMenu.setFont(new java.awt.Font("Arial", 1, 12));
        productHelpMenu.setText("Product Help");
        productHelpMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                productHelpMenuActionPerformed(evt);
            }
        });
        jMenu6.add(productHelpMenu);

        jMenuBar1.add(jMenu6);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(drawLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 1137, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jToolBar3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(displayPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(statusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 1362, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(drawLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 656, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jToolBar3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(displayPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusLabel))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public BufferedImage getLiveCam1(int num) {

        if (num == 0) {
            cvResize(imgArr[num][0], tempIm);
            return tempIm.getBufferedImage();
        } else {
            cvResize(imgArr[num][0], tempIm2);
            return tempIm2.getBufferedImage();
        }
    }

    public BufferedImage getSkin1(int num) {
        if (num == 0) {
            cvResize(imgArr[num][1], tempSkin);
            return tempSkin.getBufferedImage();
        } else {
            cvResize(imgArr[num][1], tempSkin2);
            return tempSkin2.getBufferedImage();
        }
    }

    private void deviceMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deviceMenu1ActionPerformed
        if (deviceStarted) {
            timer.restart();
        } else {
            deviceStarted = true;
            startButton.setEnabled(false);
            stopButton.setEnabled(true);

            saveImage = new BufferedImage(imWidth, imHeight, BufferedImage.TYPE_INT_ARGB);
            for (int i = 0; i < imageDb.length; i++) {
                imageDb[i] = new BufferedImage(imWidth, imHeight, BufferedImage.TYPE_INT_ARGB);
            }
            grap = (Graphics2D) saveImage.getGraphics();
            if (isFingerSet) {

                control = new SkinBasedController(left_Camera, right_Camera, this);
                thread = new Thread(control);
            } else {
                control = new ColorMeanController(left_Camera, right_Camera, this);
                thread = new Thread(control);
            }

            thread.start();
            initTimer = new Timer(50, initialTimer);
            initTimer.start();
        }
    }//GEN-LAST:event_deviceMenu1ActionPerformed

    private void stopMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopMenuActionPerformed
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
        deviceMenu1.setEnabled(true);
        stopMenu.setEnabled(false);
        thread.stop();
    }//GEN-LAST:event_stopMenuActionPerformed

    private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem9ActionPerformed
        linecolor = Color.WHITE;
    }//GEN-LAST:event_jMenuItem9ActionPerformed

    private void rightCamMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rightCamMenuActionPerformed
        viewCamPressed = true;
    }//GEN-LAST:event_rightCamMenuActionPerformed

    private void cameraButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cameraButtonActionPerformed
        if (!deviceStarted) {
            JOptionPane.showMessageDialog(drawLabel, "Please Start The Device First.", "Information", 1);
        } else if (!setCamImage) {
            JOptionPane.showMessageDialog(drawLabel, "Please wait until cameras are loaded.", "Information", 1);
        } else {
            if (viewCamPressed) {
                viewCamPressed = false;
                cameraButton.setToolTipText("Start Camera");
                leftLiveCamLabel.setIcon(null);
                leftSkinCamLabel.setIcon(null);
                rightLiveCamLabel.setIcon(null);
                rightSkinCamLabel.setIcon(null);
            } else {
                viewCamPressed = true;
                cameraButton.setToolTipText("Stop Camera");
            }

        }
    }//GEN-LAST:event_cameraButtonActionPerformed

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
        if (deviceStarted) {
            pauseButton.setEnabled(true);
            timer.restart();
        } else {
            statusLabel.setText("Status: Starting Device");
            // calibration2 = true;
//            if (!preCalibrated) {
//                isCalibrating = true;
//            }

//            CamWaiting cw = new CamWaiting(this);
//            cw.setVisible(true);
            if (this.getIsettings().calibrateNow) {
                isCalibrating = true;
            }

            if (!stylusChanged) {
                isFingerSet = this.getIsettings().fingerSelected;
            } else {
                stylusChanged = false;
            }
            //locationMapper();
            deviceStarted = true;
            startButton.setEnabled(false);
            stopButton.setEnabled(true);
            saveImage = new BufferedImage(imWidth, imHeight, BufferedImage.TYPE_INT_ARGB);
            for (int i = 0; i < imageDb.length; i++) {
                imageDb[i] = new BufferedImage(imWidth, imHeight, BufferedImage.TYPE_INT_ARGB);
            }
            grap = (Graphics2D) saveImage.getGraphics();
            if (isFingerSet) {
                control = new SkinBasedController(left_Camera, right_Camera, this);
                thread = new Thread(control);
                filterSize = 6;
            } else {
                control = new ColorMeanController(left_Camera, right_Camera, this);
                thread = new Thread(control);
            }

            thread.start();
            timer = new Timer(100, paintTimer);
            timer.start();
//            initTimer = new Timer(50, initialTimer);
//            initTimer.start();
        }
    }//GEN-LAST:event_startButtonActionPerformed

    private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopButtonActionPerformed

        if (!deviceStarted) {
            JOptionPane.showMessageDialog(drawLabel, "Please Start The Device First.", "Information", 2);
        } else {
            stopButton.setEnabled(false);
            stopMenu.setEnabled(false);
            int confirm = JOptionPane.showConfirmDialog(drawLabel, "Do You Really Want To Stop The Device?", "Confirmation", JOptionPane.YES_NO_CANCEL_OPTION);

            if (confirm == 0) {
                viewCamPressed = false;
                control.setActive(false);
                deviceStarted = false;
                setCamImage = false;
                startButton.setEnabled(true);
                imgArr = new IplImage[2][2];
                leftLiveCamLabel.setIcon(null);
                leftSkinCamLabel.setIcon(null);
                rightLiveCamLabel.setIcon(null);
                rightSkinCamLabel.setIcon(null);
                statusLabel.setText("Status: Device has stopped");
            }
        }
    }//GEN-LAST:event_stopButtonActionPerformed

    private void eraserButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eraserButtonActionPerformed
        statusLabel.setText("Status: Eraser");
        isEraserPressed = true;
        ovalSize = 25;
        linecolor = getBackground();
        blackPenButton.setEnabled(true);
        redPenButton.setEnabled(true);
        bluePenButton.setEnabled(true);
        eraserButton.setEnabled(false);
    }//GEN-LAST:event_eraserButtonActionPerformed

    private void blackPenButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_blackPenButtonActionPerformed
        isEraserPressed = false;
        statusLabel.setText("Status: Black Pen");
        ovalSize = 5;
        linecolor = Color.BLACK;
        blackPenButton.setEnabled(false);
        redPenButton.setEnabled(true);
        bluePenButton.setEnabled(true);
        eraserButton.setEnabled(true);
    }//GEN-LAST:event_blackPenButtonActionPerformed

    private void clearScreenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearScreenActionPerformed
        statusLabel.setText("Status: Clear Screen");
        saveImage.flush();
        saveImage = new BufferedImage(imWidth, imHeight, BufferedImage.TYPE_INT_ARGB);
    }//GEN-LAST:event_clearScreenActionPerformed

    private void imageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_imageButtonActionPerformed
        statusLabel.setText("Status: Inserting Background Image");
        setBackgroundImage();
    }//GEN-LAST:event_imageButtonActionPerformed

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonActionPerformed
        statusLabel.setText("Status: Next Page");
        imageDb[saveImageNum] = saveImage;
        imageDb[saveImageNum].flush();
        setImageLabel(saveImageNum, +1);
        if (saveImageNum == 4) {
            saveImageNum = -1;
        }
        saveImageNum++;
        clearSet = true;
    }//GEN-LAST:event_nextButtonActionPerformed

    private void prevButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_prevButtonActionPerformed
        statusLabel.setText("Status: Previous Page");
        imageDb[saveImageNum] = saveImage;
        imageDb[saveImageNum].flush();
        setImageLabel(saveImageNum, -1);
        if (saveImageNum == 0) {
            saveImageNum = 5;
        }
        saveImageNum--;
        clearSet = true;
    }//GEN-LAST:event_prevButtonActionPerformed

    private void redStylusMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_redStylusMenuActionPerformed
        isFingerSet = false;
    }//GEN-LAST:event_redStylusMenuActionPerformed

    private void fingerMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fingerMenuActionPerformed
        isFingerSet = true;
    }//GEN-LAST:event_fingerMenuActionPerformed

    private void saveMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveMenuActionPerformed
        File fiile = null;
        int selection = JOptionPane.showConfirmDialog(drawLabel, "Do you really want to save the images?", "Save", 1);
        if (selection == 0) {
            JFileChooser ffc = new JFileChooser();
            ffc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int returnVal = ffc.showDialog(this, "Save");
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                fiile = ffc.getSelectedFile();
            }
            imageDb[saveImageNum] = saveImage;
            saveImage.flush();
            for (int i = 0; i < imageDb.length; i++) {
                try {
                    String fileNum = fiile.getAbsolutePath() + "\\Drawing_" + (i + 1) + ".jpg";
                    ImageIO.write(imageDb[i], "jpg", new java.io.File(fileNum));
                    imageDb[i].flush();
                } catch (java.io.IOException e) {
                    System.out.println("Unable to save image:\n" + e);
                }
            }
        }
    }//GEN-LAST:event_saveMenuActionPerformed

    private void pauseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pauseButtonActionPerformed
        timer.stop();
        pauseButton.setEnabled(false);
        startButton.setEnabled(true);
    }//GEN-LAST:event_pauseButtonActionPerformed
    public static boolean mouseInPause = false;
    private void pauseButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pauseButtonMouseEntered
        // TODO add your handling code here:

        mouseInPause = true;
        if (mouseInPause) {
//            try {
//                for (int i = 0; i < 100000; i++) {
//                   // System.out.println(i);
//                }
//                rb = new Robot();
//                rb.mousePress(MouseEvent.BUTTON1_MASK);
//                rb.mouseRelease((MouseEvent.BUTTON1_MASK));
//                mouseInPause = false;
//            } catch (AWTException ex) {
//                Logger.getLogger(ProjectorGUI.class.getName()).log(Level.SEVERE, null, ex);
//            }
        }
    }//GEN-LAST:event_pauseButtonMouseEntered

    private void pauseButtonMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pauseButtonMouseExited


        mouseInPause = false;
    }//GEN-LAST:event_pauseButtonMouseExited
    public static boolean mouseInCamera = false;
    private void cameraButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cameraButtonMouseEntered
        // TODO add your handling code here:

        mouseInCamera = true;

//        try {
//            for (int i = 0; i < 100000; i++) {
//                //System.out.println(i);
//                if (!mouseInCamera) {
//                    break;
//                }
//                if (i == 99999) {
//                    rb = new Robot();
//                    rb.mousePress(MouseEvent.BUTTON1_MASK);
//                    rb.mouseRelease((MouseEvent.BUTTON1_MASK));
//                }
//            }
//
//        } catch (AWTException ex) {
//            Logger.getLogger(ProjectorGUI.class.getName()).log(Level.SEVERE, null, ex);
//        }


    }//GEN-LAST:event_cameraButtonMouseEntered

    private void cameraButtonMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cameraButtonMouseExited
        // TODO add your handling code here:

        mouseInCamera = false;
    }//GEN-LAST:event_cameraButtonMouseExited

    private void blackPenButtonMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_blackPenButtonMouseEntered
        // TODO add your handling code here:
    }//GEN-LAST:event_blackPenButtonMouseEntered

    private void redPenButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_redPenButtonActionPerformed
        isEraserPressed = false;
        statusLabel.setText("Status: Red Pen");
        ovalSize = 5;
        linecolor = Color.RED;
        blackPenButton.setEnabled(true);
        redPenButton.setEnabled(false);
        bluePenButton.setEnabled(true);
        eraserButton.setEnabled(true);
    }//GEN-LAST:event_redPenButtonActionPerformed

    private void bluePenButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bluePenButtonActionPerformed
        isEraserPressed = false;
        statusLabel.setText("Status: Blue Pen");
        ovalSize = 5;
        linecolor = Color.BLUE;
        blackPenButton.setEnabled(true);
        redPenButton.setEnabled(true);
        bluePenButton.setEnabled(false);
        eraserButton.setEnabled(true);
    }//GEN-LAST:event_bluePenButtonActionPerformed

    private void jMenu4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenu4ActionPerformed

    private void nextPageMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextPageMenuActionPerformed
        imageDb[saveImageNum] = saveImage;
        saveImage.flush();
        //System.out.println(saveImageNum);
        setImageLabel(saveImageNum, +1);
        if (saveImageNum == 4) {
            saveImageNum = -1;
        }
        saveImageNum++;
        clearSet = true;
    }//GEN-LAST:event_nextPageMenuActionPerformed

    private void prevPageMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_prevPageMenuActionPerformed
        imageDb[saveImageNum] = saveImage;
        saveImage.flush();
        setImageLabel(saveImageNum, -1);
        if (saveImageNum == 0) {
            saveImageNum = 5;
        }
        saveImageNum--;
        clearSet = true;
    }//GEN-LAST:event_prevPageMenuActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        calibrateButton = true;
        statusLabel.setText("Status: Done 5");

    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void productHelpMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_productHelpMenuActionPerformed
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dim = toolkit.getScreenSize();
        Help he = new Help();
        int x_location = (dim.width - he.getBounds().width) / 2;
        int y_location = (dim.height - he.getBounds().height) / 2;
        he.setLocation(x_location, y_location);
        he.setVisible(true);
    }//GEN-LAST:event_productHelpMenuActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        calibrateEach = true;
        butNo = 1;
        butName = "Black Pen";
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        calibrateEach = true;
        butNo = 2;
        butName = "Red Pen";
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
        calibrateEach = true;
        butNo = 3;
        butName = "Blue Pen";
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem7ActionPerformed

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed
        calibrateEach = true;
        butNo = 4;
        butName = " Eraser ";
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem8ActionPerformed

    private void jMenuItem11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem11ActionPerformed
        calibrateEach = true;
        butNo = 5;
        butName = " Clear";
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem11ActionPerformed

    private void jMenuItem12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem12ActionPerformed
        calibrateEach = true;
        butNo = 6;
        butName = " Save";
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem12ActionPerformed

    private void jMenuItem13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem13ActionPerformed
        calibrateEach = true;
        butNo = 7;
        butName = "Next";
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem13ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        calibrateEach = true;
        butNo = 8;
        butName = "Previous";
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem14ActionPerformed
        calMidBut();
        String path = JOptionPane.showInputDialog(rootPane, "Enter a name for the calibration file", "Calibration");
        saveArray(path);


        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem14ActionPerformed

    private void directoryMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_directoryMenuActionPerformed

        JFileChooser ffc = new JFileChooser();
        ffc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = ffc.showDialog(this, "Select Working Directory");
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            directory = ffc.getSelectedFile();
        }

    }//GEN-LAST:event_directoryMenuActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        if (directory != null) {
            try {
                Date dt = new Date();
                SimpleDateFormat sd = new SimpleDateFormat("E yyyy.MM.dd '@'hh.mm.ss");
                String fileNum = directory.getAbsolutePath() + "\\SurfaceImage" + sd.format(dt) + ".png";
                System.out.println(fileNum);
                ImageIO.write(saveImage, "png", new java.io.File(fileNum));
            } catch (java.io.IOException e) {
                System.out.println("Unable to save image:\n" + e);
            }
        } else {
            JOptionPane.showMessageDialog(this, "Please set the Working Directory First", "Confirmation", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_saveButtonActionPerformed

    private void jMenu6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu6ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenu6ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed

        String path = JOptionPane.showInputDialog(rootPane, "Enter the name of the calibration file", "buttonSet");
        String name;
        name = path + ".ser";

        FileInputStream fileIn = null;
        try {
            ButtonCalib jk;
            fileIn = new FileInputStream(name);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            jk = (ButtonCalib) in.readObject();
            in.close();
            fileIn.close();
            buttonMidX = jk.xVal;
            buttonMidY = jk.yVal;
            calMidBut();

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(InitialScreen.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(rootPane, "No calibration setting file named " + path + " found ", "ERROR", JOptionPane.ERROR_MESSAGE);
        } catch (IOException ex) {
            Logger.getLogger(InitialScreen.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fileIn.close();
            } catch (IOException ex) {
                Logger.getLogger(InitialScreen.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void changeStylusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changeStylusActionPerformed
        // TODO add your handling code here:

        if (!deviceStarted) {
            JOptionPane.showMessageDialog(drawLabel, "Please Start The Device First.", "Information", 2);
        } else {
            int confirm = 0;
            if (isFingerSet) {
                confirm = JOptionPane.showConfirmDialog(drawLabel, "Do You Really Want To Switch To Red Stylus?", "Confirmation", JOptionPane.YES_NO_CANCEL_OPTION);
            } else {
                confirm = JOptionPane.showConfirmDialog(drawLabel, "Do You Really Want To Switch To Finger ?", "Confirmation", JOptionPane.YES_NO_CANCEL_OPTION);
            }

            if (confirm == 0) {
                viewCamPressed = false;
                control.setActive(false);
                deviceStarted = false;
                setCamImage = false;
                startButton.setEnabled(true);
                imgArr = new IplImage[2][2];
                leftLiveCamLabel.setIcon(null);
                leftSkinCamLabel.setIcon(null);
                rightLiveCamLabel.setIcon(null);
                rightSkinCamLabel.setIcon(null);
                statusLabel.setText("Status: Device has stopped");
                isFingerSet = !isFingerSet;
                stylusChanged = true;
                startButton.doClick();

            }
        }
    }//GEN-LAST:event_changeStylusActionPerformed

    private void toolMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toolMenuActionPerformed
    }//GEN-LAST:event_toolMenuActionPerformed

    private void setMaxPointSepActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setMaxPointSepActionPerformed
        // TODO add your handling code here:
        String l = JOptionPane.showInputDialog(rootPane, "Set The Maximum Line Seperation", "100");
        maxPointSeperation = Double.valueOf(l);
    }//GEN-LAST:event_setMaxPointSepActionPerformed

    private void insertImageMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertImageMenuActionPerformed
        setBackgroundImage();
    }//GEN-LAST:event_insertImageMenuActionPerformed

    private void applyKalmanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_applyKalmanActionPerformed
        // TODO add your handling code here:
        Kalman = !Kalman;
    }//GEN-LAST:event_applyKalmanActionPerformed

    private void setWindowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setWindowActionPerformed
        // TODO add your handling code here:
        String ws = JOptionPane.showInputDialog(rootPane, "Enter Prefered Window Size? ", "5");
        int w = Integer.valueOf(ws);
        af = new ArrayFilter(w);
    }//GEN-LAST:event_setWindowActionPerformed

    private void lineWidthMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lineWidthMenuActionPerformed
       String lw = JOptionPane.showInputDialog(rootPane, "Enter Prefered Line Width ", "5");
        lineWidth = Integer.valueOf(lw);

    }//GEN-LAST:event_lineWidthMenuActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

                } catch (Exception e) {
                    System.out.println("Look and Feel doesn't available!!!");
                }
                new ProjectorGUINew().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem applyKalman;
    private javax.swing.JButton blackPenButton;
    private javax.swing.JButton bluePenButton;
    private javax.swing.JButton cameraButton;
    private javax.swing.JMenuItem changeStylus;
    private javax.swing.JButton clearScreen;
    private javax.swing.JMenuItem deviceMenu1;
    private javax.swing.JMenuItem directoryMenu;
    private javax.swing.JPanel displayPanel;
    private javax.swing.JLabel drawLabel;
    private javax.swing.JButton eraserButton;
    private javax.swing.JMenuItem fingerMenu;
    private javax.swing.JMenuItem insertImageMenu;
    private javax.swing.JButton jButton1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenu jMenu7;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem12;
    private javax.swing.JMenuItem jMenuItem13;
    private javax.swing.JMenuItem jMenuItem14;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JToolBar jToolBar3;
    private javax.swing.JLabel leftLiveCamLabel;
    private javax.swing.JLabel leftSkinCamLabel;
    private javax.swing.JMenuItem lineWidthMenu;
    private javax.swing.JButton nextButton;
    private javax.swing.JMenuItem nextPageMenu;
    private javax.swing.JButton pauseButton;
    private javax.swing.JButton prevButton;
    private javax.swing.JMenuItem prevPageMenu;
    private javax.swing.JMenuItem productHelpMenu;
    private javax.swing.JButton redPenButton;
    private javax.swing.JMenuItem redStylusMenu;
    private javax.swing.JMenuItem rightCamMenu;
    private javax.swing.JLabel rightLiveCamLabel;
    private javax.swing.JLabel rightSkinCamLabel;
    private javax.swing.JButton saveButton;
    private javax.swing.JMenuItem saveMenu;
    private javax.swing.JMenuItem setMaxPointSep;
    private javax.swing.JMenuItem setWindow;
    private javax.swing.JButton startButton;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JButton stopButton;
    private javax.swing.JMenuItem stopMenu;
    private javax.swing.JMenu toolMenu;
    // End of variables declaration//GEN-END:variables
    Action paintTimerOLD = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            int srcLength = CoordinateFinder.getLENGTH();
            grap = (Graphics2D) saveImage.getGraphics();
            grap.setBackground(drawLabel.getBackground());

            if (!clearSet) {

                int xx = srcLength - getX1();
                //       int yy = getY() - drawLabel.getLocationOnScreen().y;
                int yy = getY1();
                //yy=yy-100;
                //System.out.println("Label-"+drawLabel.getX()+","+drawLabel.getY()+"\n JPanel="+drawingPanel.getX()+","+drawingPanel.getY());
                //System.out.println("Label-"+drawLabel.getLocationOnScreen().x+","+drawLabel.getLocationOnScreen().y+"\n JPanel="+drawingPanel.getLocationOnScreen().x+","+drawingPanel.getLocationOnScreen().y);
//                if ((prevX == 0 && prevY == 0) || (xx == 0 && yy == 0)) {
//                    try {
//                        Robot rb = new Robot();
//                    } catch (AWTException ex) {
//                        Logger.getLogger(ProjectorGUINew.class.getName()).log(Level.SEVERE, null, ex);
//                    }
//                } else {
                //  g.setColor(linecolor);
                //System.out.println("prevX= " + prevX + " prevY= " + prevY + " x= " + xx + " y= " + yy);
                //  System.out.println("x= " + xx + " y= " + yy);
                //     if (xx < 800) {
                //  g.fillOval(xx, yy, 10, 10);
                // g.drawLine(prevX, prevY, xx, yy);
                grap.setColor(Color.BLUE);
                grap.drawOval(100, 100, 50, 50);
                drawing.setImage(saveImage);
                drawLabel.setIcon(drawing);
                drawLabel.repaint();
                //     } else {
                        /*
                rb.mouseMove(xx, yy);
                System.out.println("mouse moving");*/
                //    }

//                }
                prevX = xx;
                prevY = yy;

                if (viewCamPressed) {
                    liveCamViewer();
                }
            } else {
                saveImage = imageDb[saveImageNum];
                imageDb[saveImageNum].flush();
                clearSet = false;
            }
        }
    };

    public void liveCamViewer() {

        ic1.setImage(getLiveCam1(0));
        leftLiveCamLabel.setIcon(ic1);

        ic2.setImage(getSkin1(0));
        leftSkinCamLabel.setIcon(ic2);

        ic3.setImage(getLiveCam1(1));
        rightLiveCamLabel.setIcon(ic3);

        ic4.setImage(getSkin1(1));
        rightSkinCamLabel.setIcon(ic4);

        displayPanel.repaint();
    }

    public void setBackgroundImage() {
        if (fc == null) {
            fc = new JFileChooser();

            //     fc.addChoosableFileFilter(new ImageFilter());
            try {
                fc.setAcceptAllFileFilterUsed(false);
                fc.setFileFilter(new ImageFilter());
                fc.setAccessory(new ImagePreview(fc));
            } catch (IllegalArgumentException exc) {
                JOptionPane.showMessageDialog(this, "No file was selected", "No File", JOptionPane.INFORMATION_MESSAGE);
            }
        }
        int returnVal = fc.showDialog(this, "Open");

        try {
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                file = fc.getSelectedFile();

            }

            fc.setSelectedFile(null);
            try {
                bf = ImageIO.read(file);
                //   System.out.println("read done");
            } catch (IOException ex) {
                Logger.getLogger(ProjectorGUINew.class.getName()).log(Level.SEVERE, null, ex);
            }

            Image newbg = bf.getScaledInstance(imWidth, imHeight, Image.SCALE_AREA_AVERAGING);
            System.out.println("Cameeeee");
            saveImage = (BufferedImage) newbg;
            bf.flush();
        } catch (IllegalArgumentException ex) {
        }
    }

    public void setImageLabel(int num, int sign) {
        switch (num + 1) {
            case 1:
                // jButton1.setEnabled(false);

                if (sign == +1) {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[1]));
                    jButton1.repaint();
                } else {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[4]));
                    jButton1.repaint();
                }
                break;
            case 2:

                if (sign == +1) {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[2]));
                    jButton1.repaint();
                } else {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[0]));
                    jButton1.repaint();
                }
                break;
            case 3:

                if (sign == +1) {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[3]));
                    jButton1.repaint();
                } else {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[1]));
                    jButton1.repaint();
                }
                break;
            case 4:

                if (sign == +1) {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[4]));
                    jButton1.repaint();
                } else {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[2]));
                    jButton1.repaint();
                }
                break;
            case 5: //  jButton5.setEnabled(false);
                if (sign == +1) {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[0]));
                    jButton1.repaint();
                } else {
                    jButton1.setIcon(new javax.swing.ImageIcon(iconDb[3]));
                    jButton1.repaint();
                }
                break;
        }
    }
    Action initialTimer = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            try {
                BufferedImage ani = null;
                BufferedImage subAni = null;

                ani = ImageIO.read(new File((getClass().getResource("/UserInterface/board.jpg")).getPath()));

                int inity = ani.getHeight() / 10;

                subAni = ani.getSubimage(0, 0, ani.getWidth(), (ii + 1) * inity);

                drawLabel.setIcon(new javax.swing.ImageIcon(subAni));
                drawLabel.repaint();
                Thread.sleep(500);
                if (ii == 9) {
                    Thread.sleep(1000);
                    initTimer.stop();
                    timer = new Timer(15, paintTimer);
                    timer.start();
                }
                ii++;
                ani.flush();
                subAni.flush();

            } catch (InterruptedException ex) {
                Logger.getLogger(ProjectorGUINew.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(ProjectorGUINew.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    };
    int shashiX, shashiY, shashiStatus = 1, shashiCount = 0;
    boolean restor = false;
    Action paintTimer = new AbstractAction() {

        public void actionPerformed(ActionEvent event) {
            srcLength = CoordinateFinder.getLENGTH();
            srcHeight = CoordinateFinder.getHEIGHT();

            int valX = srcLength - getX1();
            int valY = getY1();
            //System.out.println("x= " + valX + " y= " + valY);
            grap = (Graphics2D) saveImage.getGraphics();
            grap.setBackground(drawLabel.getBackground());
            if (viewCamPressed) {
                liveCamViewer();
            }
            if (isCalibrating) {
                calibrateSeven3(valX, valY);
            } else if (findingLimit) {
                findingYLimit(valX, valY);
            } else if (calibrateButton) {
                buttonCalibration(valX, valY);
            } else if (calibrateEach) {
                calibrateButton(valX, valY);
            } else if (!clearSet) {
                if ((valX == srcLength + 10) || (valY == -10)) {
                    if (shashiStatus == 1) {
//                        if (shashiCount == 0) {
//                            shashiX = prevX;
//                            shashiY = prevY;
//                        }
//                        doPaint2(shashiX, shashiY);
//                        shashiCount++;
//                        if (shashiCount == filterSize) {
//                            shashiStatus = 0;
//                            shashiCount = 0;
//                            af=new ArrayFilter(filterSize);
////                           af.restore();
//                      shashiCount++;
//                       if (shashiCount == filterSize / 2) {
                        af = new ArrayFilter(filterSize);
                        af2 = new ArrayFilter(filterSize);
                        //kl=new ShashKalmanTest();
                        restor = true;
                        shashiStatus = 2;
//                            shashiCount=0;
//                       }else{
//                            //doPaint2(prevX, prevY);
//                        }
//                        }
                    }

                    prevX = imWidth;
                    prevY = imHeight;

                    drawing.setImage(saveImage);
                    drawLabel.setIcon(drawing);
                    drawLabel.repaint();
                } else {
                    shashiStatus = 1;
                    doPaint2(valX, valY);
                    //doPaintLimit(valX, valY);
                    //System.out.println("In Paint");
                }

            } else {
                saveImage = imageDb[saveImageNum];
                imageDb[saveImageNum].flush();
                clearSet = false;
            }
        }
    };

    private double getAverage(int[] ab) {

        Arrays.sort(ab);
        double fg = 0;

        for (int jkl : ab) {
            System.out.print(jkl + " ");
        }
        System.out.println("");
        for (int hr = 11; hr <= 20; hr++) {
            fg = fg + ab[hr];
        }

        return fg / 10;
    }

    public boolean isStable(int[] arX, int[] arY) {
        Arrays.sort(arX);
        Arrays.sort(arY);
        int kot = arX[20] - arX[11];
        int uol = arY[20] - arY[11];

        if (kot > 15 || uol > 15) {
            return false;
        } else {
            return true;
        }
    }

    private void gestureAction(int gesture) {
        switch (gesture) {
            case 0:
                break;

            case +1:
//                undo();
//                statusLabel.setText("Status: Slide Right");
//                imageDb[saveImageNum] = saveImage;
//                saveImage.flush();
//                //System.out.println(saveImageNum);
//                setImageLabel(saveImageNum, +1);
//                if (saveImageNum == 4) {
//                    saveImageNum = -1;
//                }
//                saveImageNum++;
//                clearSet = true;
                nextButton.doClick();
                break;

            case -1:
                undo();
//                imageDb[saveImageNum] = saveImage;
//                saveImage.flush();
//                setImageLabel(saveImageNum, -1);
//                if (saveImageNum == 0) {
//                    saveImageNum = 5;
//                }
//                saveImageNum--;
//                clearSet = true;
                prevButton.doClick();
                break;

            case +2:
                undo();
                System.out.println("Scroll up");
                break;

            case -2:
                undo();
                System.out.println("Scroll Down");
                //clearScreen.doClick();
                break;
        }
    }

    public void undo() {
        for (int k = 0; k < pointDb.length; k++) {
            grap.setColor(getBackground());
            grap.fillOval(pointDb[k][0], pointDb[k][1], 10, 10);
        }
    }

    public void doPaint(int xx, int yy) {
        //System.out.println(xx + "," + yy);
        //xx = (int) (aw * xx * xx + bw * yy * yy + cw * xx + dw * yy + ew);
        //yy = (int) (fw * xx * xx + gw * yy * yy + hw * xx + iw * yy + jw);
        // System.out.println(a+","+b+","+c+","+d+","+e+","+f);
        xx = (int) (a * xx + b * yy + c);
        yy = (int) (d * xx + e * yy + f);
//                int[] lk={xx,yy};
//                int[] pl = kalman.kalman(lk);
//                xx=pl[0];
//                yy=pl[1];
//                System.out.println(xx+","+yy);
//                //yy=yy-100;
        //System.out.println("Label-"+drawLabel.getX()+","+drawLabel.getY()+"\n JPanel="+drawingPanel.getX()+","+drawingPanel.getY());
        //System.out.println("Label-"+drawLabel.getLocationOnScreen().x+","+drawLabel.getLocationOnScreen().y+"\n JPanel="+drawingPanel.getLocationOnScreen().x+","+drawingPanel.getLocationOnScreen().y);
        if ((prevX == 0 && prevY == 0) || (xx == 0 && yy == 0)) {
        } else {
            grap.setColor(linecolor);
            pointDb[frameNo][0] = xx;
            pointDb[frameNo][1] = yy;
            frameNo++;
            frameNo = frameNo % frameCount;
            //System.out.println("prevX= " + prevX + " prevY= " + prevY + " x= " + xx + " y= " + yy);
            //      System.out.println("x= " + xx + " y= " + yy);
            //     if (xx < 800) {

            grap.fillOval(xx, yy, ovalSize, ovalSize);
            //  grap.drawLine(xx, yy, prevX, prevY);
            coordinate[0] = xx;
            coordinate[1] = yy;
            buttonPressing(coordinate);
            gestureType = sl.identifyGestureType(coordinate);
            gestureAction(gestureType);
            //g.drawLine(prevX, prevY, xx, yy);
            drawing.setImage(saveImage);
            drawLabel.setIcon(drawing);
            drawLabel.repaint();
            //     } else {
                        /*
            rb.mouseMove(xx, yy);
            System.out.println("mouse moving");*/
            //    }
        }
        prevX = xx;
        prevY = yy;
    }

    public void doPaint1(int xx, int yy) {
        //System.out.println(xx + "," + yy);
        //xx = (int) (aw * xx * xx + bw * yy * yy + cw * xx + dw * yy + ew);
        //yy = (int) (fw * xx * xx + gw * yy * yy + hw * xx + iw * yy + jw);
        // System.out.println(a+","+b+","+c+","+d+","+e+","+f);
        xx = (int) (a * xx * xx + b * xx + c * yy + d);
        yy = (int) (e * yy * yy + f * xx + g * yy + h);
//                int[] lk={xx,yy};
//                int[] pl = kalman.kalman(lk);
//                xx=pl[0];
//                yy=pl[1];
//                System.out.println(xx+","+yy);
//                //yy=yy-100;
        //System.out.println("Label-"+drawLabel.getX()+","+drawLabel.getY()+"\n JPanel="+drawingPanel.getX()+","+drawingPanel.getY());
        //System.out.println("Label-"+drawLabel.getLocationOnScreen().x+","+drawLabel.getLocationOnScreen().y+"\n JPanel="+drawingPanel.getLocationOnScreen().x+","+drawingPanel.getLocationOnScreen().y);
        if ((prevX == 0 && prevY == 0) || (xx == 0 && yy == 0)) {
        } else {

            grap.setColor(linecolor);
            pointDb[frameNo][0] = xx;
            pointDb[frameNo][1] = yy;
            frameNo++;
            frameNo = frameNo % frameCount;

            //System.out.println("prevX= " + prevX + " prevY= " + prevY + " x= " + xx + " y= " + yy);
            //      System.out.println("x= " + xx + " y= " + yy);
            //     if (xx < 800) {
            grap.fillOval(xx, yy, 10, 10);
            //  g.drawLine(xx, yy, prevX, prevY);
            coordinate[0] = xx;
            coordinate[1] = yy;
            gestureType = sl.identifyGestureType(coordinate);
            gestureAction(gestureType);
            //g.drawLine(prevX, prevY, xx, yy);
            drawing.setImage(saveImage);
            drawLabel.setIcon(drawing);
            drawLabel.repaint();
            //     } else {
                        /*
            rb.mouseMove(xx, yy);
            System.out.println("mouse moving");*/
            //    }

        }
        prevX = xx;
        prevY = yy;


    }
    int filterSize = 4;
    ArrayFilter af = new ArrayFilter(filterSize);
    ArrayFilter af2 = new ArrayFilter(filterSize);
    ShashKalmanTest kl = new ShashKalmanTest();

    public void doPaint2(int xx, int yy) {
        coordinate[0] = xx;
        coordinate[1] = yy;
        buttonPressing(coordinate);
        xx = (int) (a * xx * xx + b * yy * yy + c * xx + d * yy + e);
        yy = (int) (f * xx * xx + g * yy * yy + h * xx + i * yy + j);
        int[] po;
//        if (!Kalman) {
        if (isFingerSet) {
            po = af.movingAverage(xx, yy);
            //po = af.getMedian(xx, yy);
        } else {
            po = af.movingAverage(xx, yy);
        }
//        } else {
//            po=kl.getValue(xx, yy);
//        }
        xx = po[0];
        yy = po[1];

        if (restor) {
            System.out.println(xx + "," + yy);
        }
        if (((prevX >= imWidth || prevY >= imHeight) || (xx >= imWidth || yy >= imHeight))) {
            //   System.out.println("came here");
            grap.setColor(linecolor);
            grap.setStroke(new BasicStroke(10));
            pointDb[frameNo][0] = xx;
            pointDb[frameNo][1] = yy;
            frameNo++;
            frameNo = frameNo % frameCount;

            //gestureType = sl.identifyGestureType(coordinate);
            //gestureAction(gestureType);
            //grap.drawLine(xx, yy, xx, yy);
            drawing.setImage(saveImage);
            drawLabel.setIcon(drawing);
            drawLabel.repaint();

        } else {
            // System.out.println("came here 2");
            grap.setColor(linecolor);
            grap.setStroke(new BasicStroke(lineWidth));
            pointDb[frameNo][0] = xx;
            pointDb[frameNo][1] = yy;
            frameNo++;
            frameNo = frameNo % frameCount;

            //gestureType = sl.identifyGestureType(coordinate);
            // gestureAction(gestureType);
            double dis = Math.sqrt(Math.pow((prevX - xx), 2) + Math.pow((prevY - yy), 2));
            if (dis > maxPointSeperation) {
                System.out.println(maxPointSeperation);
            } else {
                if (!isEraserPressed) {
                    grap.drawLine(prevX, prevY, xx, yy);
                } else {
                    grap.fillOval(xx, yy, 30, 30);
                }

            }

            drawing.setImage(saveImage);
            drawLabel.setIcon(drawing);
            drawLabel.repaint();
        }
        prevX = xx;
        prevY = yy;
    }

    public void doPaintLimit(int xx, int yy) {
        coordinate[0] = xx;
        coordinate[1] = yy;
        buttonPressing(coordinate);
        xx = (int) (a * xx * xx + b * yy * yy + c * xx + d * yy + e);
        yy = (int) (f * xx * xx + g * yy * yy + h * xx + i * yy + j);
        int[] po;
        po = af2.movingAverage(xx, yy);

        xx = po[0];
        yy = po[1];

        if (restor) {
            System.out.println(xx + "," + yy);
        }
        if (((prevX >= imWidth || prevY >= imHeight) || (xx >= imWidth || yy >= imHeight))) {
            //   System.out.println("came here");
            grap.setColor(Color.yellow);
            grap.setStroke(new BasicStroke(10));
            pointDb[frameNo][0] = xx;
            pointDb[frameNo][1] = yy;
            frameNo++;
            frameNo = frameNo % frameCount;

            //gestureType = sl.identifyGestureType(coordinate);
            //gestureAction(gestureType);
            //grap.drawLine(xx, yy, xx, yy);
            drawing.setImage(saveImage);
            drawLabel.setIcon(drawing);
            drawLabel.repaint();

        } else {
            // System.out.println("came here 2");
            grap.setColor(Color.yellow);
            grap.setStroke(new BasicStroke(3));
            pointDb[frameNo][0] = xx;
            pointDb[frameNo][1] = yy;
            frameNo++;
            frameNo = frameNo % frameCount;

            //gestureType = sl.identifyGestureType(coordinate);
            // gestureAction(gestureType);
            double dis = Math.sqrt(Math.pow((prevX - xx), 2) + Math.pow((prevY - yy), 2));
            if (dis > maxPointSeperation) {
                System.out.println(maxPointSeperation);
            } else {
                grap.drawLine(prevX, prevY, xx, yy);
            }

            drawing.setImage(saveImage);
            drawLabel.setIcon(drawing);
            drawLabel.repaint();
        }
        prevX = xx;
        prevY = yy;
    }

    public void findingYLimit(int xx, int yy) {
        xx = (int) (a * xx * xx + b * yy * yy + c * xx + d * yy + e);
        yy = (int) (f * xx * xx + g * yy * yy + h * xx + i * yy + j);
        // grap = (Graphics2D) saveImage.getGraphics();
        //System.out.println("Came here" + xx + "," + yy);

        if (yy > 750) {
            grap.setColor(Color.GREEN);
            statusLabel.setText("Status: Calibrating");
            control.setGettingLast(true);
            if (control.isFinishedY()) {
                findingLimit = false;
                control.setGettingLast(false);
            }
            //findingLimit = control.isFinishedY();

        } else {
            control.setGettingLast(false);
            grap.setColor(Color.BLACK);
        }

        grap.fillOval(400, 500, 20, 20);
        grap.drawOval(370, 530, 80, 80);
//                g.drawLine(370, 555, 725, 555);
//                g.drawLine(705, 535, 705, 575);
        drawing.setImage(saveImage);
        drawLabel.setIcon(drawing);
        drawLabel.repaint();
    }

    public void calibrate(int valX, int valY) {
        isCal1 = inRange1(valX, valY);
        isCal2 = inRange2(valX, valY);
        isCal3 = inRange3(valX, valY);


        switch (state) {
            case 0:
                if (finished1 && finished2 && finished3) {
                    state = 4;


                } else if (isCal1 && !finished1) {
                    count = 0;
                    state = 1;


                } else if (isCal2 && !finished2) {
                    count = 0;
                    state = 2;


                } else if (isCal3 && !finished3) {
                    count = 0;
                    state = 3;


                }
                break;


            case 1:
                if (!isCal1) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished1 = true;
                    x1 = (int) getAverage(xVals);
                    y1 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 2:
                if (!isCal2) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished2 = true;
                    x2 = (int) getAverage(xVals);
                    y2 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 3:
                if (!isCal3) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished3 = true;
                    x3 = (int) getAverage(xVals);
                    y3 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 4: //finished calibrating
                CalibrateTouch ct = new CalibrateTouch();


                double[] rt = ct.getMatrix(x1, y1, x2, y2, x3, y3, 400, 200, 100, 550, 700, 550);
                a = rt[0];
                b = rt[1];
                c = rt[2];
                d = rt[3];
                e = rt[4];
                f = rt[5];

                isCalibrating = false;
                findingLimit = false;
                getSettings().setA(a);
                getSettings().setB(b);
                getSettings().setC(c);
                getSettings().setD(d);
                getSettings().setE(e);
                getSettings().setF(f);

                saveImage.flush();
                saveImage = new BufferedImage(imWidth, imHeight, BufferedImage.TYPE_INT_ARGB);


                break;


        }

        if (finished1) {
            grap.setColor(Color.GREEN);
            statusLabel.setText("Status: Done");


        } else if (isCal1) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);



        }
        grap.fillOval(400, 200, 10, 10);
        grap.drawOval(385, 185, 40, 40);
        grap.drawLine(385, 205, 425, 205);
        grap.drawLine(405, 185, 405, 225);



        if (finished2) {
            grap.setColor(Color.GREEN);
            statusLabel.setText("Status: Done");


        } else if (isCal2) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(100, 550, 10, 10);
        grap.drawOval(85, 535, 40, 40);
        grap.drawLine(85, 555, 125, 555);
        grap.drawLine(105, 535, 105, 575);



        if (finished3) {
            grap.setColor(Color.GREEN);
            statusLabel.setText("Status: Done");


        } else if (isCal3) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(700, 550, 10, 10);
        grap.drawOval(685, 535, 40, 40);
        grap.drawLine(685, 555, 725, 555);
        grap.drawLine(705, 535, 705, 575);
        drawing.setImage(saveImage);
        drawLabel.setIcon(drawing);
        drawLabel.repaint();


    }

    public void calibrate5(int valX, int valY) {
        isCal1 = range1(valX, valY);
        isCal2 = range2(valX, valY);
        isCal3 = range3(valX, valY);
        isCal4 = range4(valX, valY);
        isCal5 = range5(valX, valY);



        switch (state) {
            case 0:
                if (finished1 && finished2 && finished3 && finished4 & finished5) {
                    state = 6;


                } else if (isCal1 && !finished1) {
                    count = 0;
                    state = 1;


                } else if (isCal2 && !finished2) {
                    count = 0;
                    state = 2;


                } else if (isCal3 && !finished3) {
                    count = 0;
                    state = 3;


                } else if (isCal4 && !finished4) {
                    count = 0;
                    state = 4;


                } else if (isCal5 && !finished5) {
                    count = 0;
                    state = 5;


                }
                break;


            case 1:
                if (!isCal1) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished1 = true;
                    statusLabel.setText("Status: Done 1");
                    System.out.println("Done 1");
                    x1 = (int) getAverage(xVals);
                    y1 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 2:
                if (!isCal2) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished2 = true;
                    statusLabel.setText("Status: Done 2");
                    System.out.println("Done 2");
                    x2 = (int) getAverage(xVals);
                    y2 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 3:
                if (!isCal3) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished3 = true;
                    statusLabel.setText("Status: Done 3");
                    System.out.println("Done 3");
                    x3 = (int) getAverage(xVals);
                    y3 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 4:
                if (!isCal4) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished4 = true;
                    statusLabel.setText("Status: Done 4");
                    System.out.println("Done 4");
                    x4 = (int) getAverage(xVals);
                    y4 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 5:
                if (!isCal5) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished5 = true;
                    statusLabel.setText("Status: Done 5");
                    System.out.println("Done 5");
                    x5 = (int) getAverage(xVals);
                    y5 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;
                    state = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 6: //finished calibrating
                CalibrateTouch ct = new CalibrateTouch();


                double[] rt = ct.getMatrix5(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, 300, 250, 600, 250, 225, 600, 450, 600, 675, 600);
                System.out.println("300,250=" + x1 + "," + y1);
                System.out.println("600,250=" + x2 + "," + y2);
                System.out.println("225,600=" + x3 + "," + y3);
                System.out.println("450,600=" + x4 + "," + y4);
                System.out.println("675,600=" + x5 + "," + y5);
                a = rt[0];
                b = rt[1];
                c = rt[2];
                d = rt[3];
                e = rt[4];
                f = rt[5];

                System.out.println("x=" + a + "x+" + b + "y+" + c + "");
                System.out.println("y=" + d + "x+" + e + "y+" + f + "");

                isCalibrating = false;
                findingLimit = false;
                getSettings().setA(a);
                getSettings().setB(b);
                getSettings().setC(c);
                getSettings().setD(d);
                getSettings().setE(e);
                getSettings().setF(f);
                saveImage.flush();
                saveImage = new BufferedImage(imWidth, imHeight, BufferedImage.TYPE_INT_ARGB);


                break;


        }

        if (finished1) {
            grap.setColor(Color.GREEN);



        } else if (isCal1) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);



        }
        grap.fillOval(300, 250, 10, 10);
        grap.drawOval(285, 235, 40, 40);
        grap.drawLine(285, 255, 325, 255);
        grap.drawLine(305, 235, 305, 275);



        if (finished2) {
            grap.setColor(Color.GREEN);



        } else if (isCal2) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(600, 250, 10, 10);
        grap.drawOval(585, 235, 40, 40);
        grap.drawLine(585, 255, 625, 255);
        grap.drawLine(605, 235, 605, 275);



        if (finished3) {
            grap.setColor(Color.GREEN);



        } else if (isCal3) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(225, 600, 10, 10);
        grap.drawOval(210, 585, 40, 40);
        grap.drawLine(210, 605, 250, 605);
        grap.drawLine(230, 585, 230, 625);



        if (finished4) {
            grap.setColor(Color.GREEN);



        } else if (isCal4) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(450, 600, 10, 10);
        grap.drawOval(435, 585, 40, 40);
        grap.drawLine(435, 605, 475, 605);
        grap.drawLine(455, 585, 455, 625);



        if (finished5) {
            grap.setColor(Color.GREEN);



        } else if (isCal5) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(675, 600, 10, 10);
        grap.drawOval(660, 585, 40, 40);
        grap.drawLine(660, 605, 700, 605);
        grap.drawLine(680, 585, 680, 625);

        drawing.setImage(saveImage);
        drawLabel.setIcon(drawing);
        drawLabel.repaint();


    }

    public void calibrateEight(int valX, int valY) {
        isCal1 = range1(valX, valY);
        isCal2 = range2(valX, valY);
        isCal3 = range3(valX, valY);
        isCal4 = range4(valX, valY);
        isCal5 = range5(valX, valY);



        switch (state) {
            case 0:
                if (finished1 && finished2 && finished3 && finished4 & finished5) {
                    state = 6;


                } else if (isCal1 && !finished1) {
                    count = 0;
                    state = 1;


                } else if (isCal2 && !finished2) {
                    count = 0;
                    state = 2;


                } else if (isCal3 && !finished3) {
                    count = 0;
                    state = 3;


                } else if (isCal4 && !finished4) {
                    count = 0;
                    state = 4;


                } else if (isCal5 && !finished5) {
                    count = 0;
                    state = 5;


                }
                break;


            case 1:
                if (!isCal1) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished1 = true;
                    x1 = (int) getAverage(xVals);
                    y1 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 2:
                if (!isCal2) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished2 = true;
                    x2 = (int) getAverage(xVals);
                    y2 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 3:
                if (!isCal3) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished3 = true;
                    x3 = (int) getAverage(xVals);
                    y3 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 4:
                if (!isCal4) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished4 = true;
                    x4 = (int) getAverage(xVals);
                    y4 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 5:
                if (!isCal5) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished5 = true;
                    x5 = (int) getAverage(xVals);
                    y5 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 6: //finished calibrating
                CalibrateTouch ct = new CalibrateTouch();


                double[] rt = ct.get8Matrix(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, 300, 250, 600, 250, 225, 600, 450, 600, 675, 600);
                a = rt[0];
                b = rt[1];
                c = rt[2];
                d = rt[3];
                e = rt[4];
                f = rt[5];
                g = rt[6];
                h = rt[7];

                System.out.println("x=" + a + "x^2+" + b + "x" + c + "y" + d + "");
                System.out.println("y=" + e + "y^2+" + f + "x" + g + "y" + h + "");

                isCalibrating = false;
                findingLimit = false;
                getSettings().setA(a);
                getSettings().setB(b);
                getSettings().setC(c);
                getSettings().setD(d);
                getSettings().setE(e);
                getSettings().setF(f);
                saveImage.flush();
                saveImage = new BufferedImage(imWidth, imHeight, BufferedImage.TYPE_INT_ARGB);


                break;


        }


        if (finished1) {
            grap.setColor(Color.GREEN);
            statusLabel.setText("Status: Done");


        } else if (isCal1) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);



        }
        grap.fillOval(300, 250, 10, 10);
        grap.drawOval(285, 235, 40, 40);
        grap.drawLine(285, 255, 325, 255);
        grap.drawLine(305, 235, 305, 275);



        if (finished2) {
            grap.setColor(Color.GREEN);
            statusLabel.setText("Status: Done");


        } else if (isCal2) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(600, 250, 10, 10);
        grap.drawOval(585, 235, 40, 40);
        grap.drawLine(585, 255, 625, 255);
        grap.drawLine(605, 235, 605, 275);



        if (finished3) {
            grap.setColor(Color.GREEN);
            statusLabel.setText("Status: Done");


        } else if (isCal3) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(225, 600, 10, 10);
        grap.drawOval(210, 585, 40, 40);
        grap.drawLine(210, 605, 250, 605);
        grap.drawLine(230, 585, 230, 625);



        if (finished4) {
            grap.setColor(Color.GREEN);
            statusLabel.setText("Status: Done");


        } else if (isCal4) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(450, 600, 10, 10);
        grap.drawOval(435, 585, 40, 40);
        grap.drawLine(435, 605, 475, 605);
        grap.drawLine(455, 585, 455, 625);



        if (finished5) {
            grap.setColor(Color.GREEN);
            statusLabel.setText("Status: Done");


        } else if (isCal5) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(675, 600, 10, 10);
        grap.drawOval(660, 585, 40, 40);
        grap.drawLine(660, 605, 700, 605);
        grap.drawLine(680, 585, 680, 625);

        drawing.setImage(saveImage);
        drawLabel.setIcon(drawing);
        drawLabel.repaint();


    }
    Calibrate7 cal = new Calibrate7();

    public void calibrateSeven(int valX, int valY) {

        isCal1 = cal.point1(valX, valY);
        isCal2 = cal.point2(valX, valY);
        isCal3 = cal.point3(valX, valY);
        isCal4 = cal.point4(valX, valY);
        isCal5 = cal.point5(valX, valY);
        isCal6 = cal.point6(valX, valY);
        isCal7 = cal.point7(valX, valY);



        switch (state) {
            case 0:
                if (finished1 && finished2 && finished3 && finished4 && finished5 && finished6 && finished7) {
                    state = 8;


                } else if (isCal1 && !finished1) {
                    count = 0;
                    state = 1;
                    System.out.println("State 1");


                } else if (isCal2 && !finished2) {
                    count = 0;
                    state = 2;
                    System.out.println("State 2");


                } else if (isCal3 && !finished3) {
                    count = 0;
                    state = 3;
                    System.out.println("State 3");


                } else if (isCal4 && !finished4) {
                    count = 0;
                    state = 4;
                    System.out.println("State 4");


                } else if (isCal5 && !finished5) {
                    count = 0;
                    state = 5;
                    System.out.println("State 5");


                } else if (isCal6 && !finished6) {
                    count = 0;
                    state = 6;
                    System.out.println("State 6");


                } else if (isCal7 && !finished7) {
                    count = 0;
                    state = 7;
                    System.out.println("State 7");


                }
                break;


            case 1:
                if (!isCal1) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished1 = true;
                    statusLabel.setText("Status: Done 1");
                    System.out.println("done 1");
                    x1 = (int) getAverage(xVals);
                    y1 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 2:
                if (!isCal2) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished2 = true;
                    statusLabel.setText("Status: Done 2");
                    System.out.println("done 2");
                    x2 = (int) getAverage(xVals);
                    y2 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 3:
                if (!isCal3) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished3 = true;
                    statusLabel.setText("Status: Done 3");
                    System.out.println("done 3");
                    x3 = (int) getAverage(xVals);
                    y3 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 4:
                if (!isCal4) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished4 = true;
                    statusLabel.setText("Status: Done 4");
                    System.out.println("done 4");
                    x4 = (int) getAverage(xVals);
                    y4 = (int) getAverage(yVals);
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 5:
                if (!isCal5) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished5 = true;
                    statusLabel.setText("Status: Done 5");
                    System.out.println("done 5");
                    x5 = (int) getAverage(xVals);
                    y5 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 6:
                if (!isCal6) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished6 = true;
                    statusLabel.setText("Status: Done 6");
                    System.out.println("done 6");
                    x6 = (int) getAverage(xVals);
                    y6 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 7:
                if (!isCal7) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished7 = true;
                    statusLabel.setText("Status: Done 7");
                    System.out.println("done 7");
                    x7 = (int) getAverage(xVals);
                    y7 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 8: //finished calibrating
                CalibrateTouch ct = new CalibrateTouch();


                double[] rt = ct.getMatrix7(x1, y1, x2, y2, x3, y3, x4, y4,
                        x5, y5, x6, y6, x7, y7,
                        100, 250, 400, 150, 700, 150,
                        1000, 250, 200, 600, 500, 500, 800, 600);

                System.out.println("100,250=" + x1 + "," + y1);
                System.out.println("400,150=" + x2 + "," + y2);
                System.out.println("700,150=" + x3 + "," + y3);
                System.out.println("1000,250=" + x4 + "," + y4);
                System.out.println("200,600=" + x5 + "," + y5);
                System.out.println("500,500=" + x6 + "," + y6);
                System.out.println("800,600=" + x7 + "," + y7);

                a = rt[0];
                b = rt[1];
                c = rt[2];
                d = rt[3];
                e = rt[4];
                f = rt[5];

                System.out.println("x=" + a + "x+" + b + "y+" + c + "");
                System.out.println("y=" + d + "x+" + e + "y+" + f + "");

                isCalibrating = false;
                findingLimit = false;
                getSettings().setA(a);
                getSettings().setB(b);
                getSettings().setC(c);
                getSettings().setD(d);
                getSettings().setE(e);
                getSettings().setF(f);
                saveImage.flush();
                saveImage = new BufferedImage(imWidth, imHeight, BufferedImage.TYPE_INT_ARGB);


                break;


        }

        if (finished1) {
            grap.setColor(Color.GREEN);


        } else if (isCal1) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(100, 250, 10, 10);
        grap.drawOval(85, 235, 40, 40);
        grap.drawLine(85, 255, 125, 255);
        grap.drawLine(105, 235, 105, 275);



        if (finished2) {
            grap.setColor(Color.GREEN);



        } else if (isCal2) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(400, 150, 10, 10);
        grap.drawOval(385, 135, 40, 40);
        grap.drawLine(385, 155, 425, 155);
        grap.drawLine(405, 135, 405, 175);



        if (finished3) {
            grap.setColor(Color.GREEN);



        } else if (isCal3) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(700, 150, 10, 10);
        grap.drawOval(685, 135, 40, 40);
        grap.drawLine(685, 155, 725, 155);
        grap.drawLine(705, 135, 705, 175);



        if (finished4) {
            grap.setColor(Color.GREEN);



        } else if (isCal4) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(1000, 250, 10, 10);
        grap.drawOval(985, 235, 40, 40);
        grap.drawLine(985, 255, 1025, 255);
        grap.drawLine(1005, 235, 1005, 275);



        if (finished5) {
            grap.setColor(Color.GREEN);



        } else if (isCal5) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(200, 600, 10, 10);
        grap.drawOval(185, 585, 40, 40);
        grap.drawLine(185, 605, 225, 605);
        grap.drawLine(205, 585, 205, 625);



        if (finished6) {
            grap.setColor(Color.GREEN);


        } else if (isCal6) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(500, 500, 10, 10);
        grap.drawOval(485, 485, 40, 40);
        grap.drawLine(485, 505, 525, 505);
        grap.drawLine(505, 485, 505, 525);



        if (finished7) {
            grap.setColor(Color.GREEN);


        } else if (isCal7) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(800, 600, 10, 10);
        grap.drawOval(785, 585, 40, 40);
        grap.drawLine(785, 605, 825, 605);
        grap.drawLine(805, 585, 805, 625);


        drawing.setImage(saveImage);
        drawLabel.setIcon(drawing);
        drawLabel.repaint();


    }

    public void calibrateSeven2(int valX, int valY) {

        isCal1 = cal.point1(valX, valY);
        isCal2 = cal.point2(valX, valY);
        isCal3 = cal.point3(valX, valY);
        isCal4 = cal.point4(valX, valY);
        isCal5 = cal.point5(valX, valY);
        isCal6 = cal.point6(valX, valY);
        isCal7 = cal.point7(valX, valY);



        switch (state) {
            case 0:
                if (finished1 && finished2 && finished3 && finished4 && finished5 && finished6 && finished7) {
                    state = 8;


                } else if (isCal1 && !finished1) {
                    count = 0;
                    state = 1;
                    System.out.println("State 1");


                } else if (isCal2 && !finished2) {
                    count = 0;
                    state = 2;
                    System.out.println("State 2");


                } else if (isCal3 && !finished3) {
                    count = 0;
                    state = 3;
                    System.out.println("State 3");


                } else if (isCal4 && !finished4) {
                    count = 0;
                    state = 4;
                    System.out.println("State 4");


                } else if (isCal5 && !finished5) {
                    count = 0;
                    state = 5;
                    System.out.println("State 5");


                } else if (isCal6 && !finished6) {
                    count = 0;
                    state = 6;
                    System.out.println("State 6");


                } else if (isCal7 && !finished7) {
                    count = 0;
                    state = 7;
                    System.out.println("State 7");


                }
                break;


            case 1:
                if (!isCal1) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished1 = true;
                    statusLabel.setText("Status: Done 1");
                    System.out.println("done 1");
                    x1 = (int) getAverage(xVals);
                    y1 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 2:
                if (!isCal2) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished2 = true;
                    statusLabel.setText("Status: Done 2");
                    System.out.println("done 2");
                    x2 = (int) getAverage(xVals);
                    y2 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 3:
                if (!isCal3) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished3 = true;
                    statusLabel.setText("Status: Done 3");
                    System.out.println("done 3");
                    x3 = (int) getAverage(xVals);
                    y3 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 4:
                if (!isCal4) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished4 = true;
                    statusLabel.setText("Status: Done 4");
                    System.out.println("done 4");
                    x4 = (int) getAverage(xVals);
                    y4 = (int) getAverage(yVals);
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 5:
                if (!isCal5) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished5 = true;
                    statusLabel.setText("Status: Done 5");
                    System.out.println("done 5");
                    x5 = (int) getAverage(xVals);
                    y5 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 6:
                if (!isCal6) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished6 = true;
                    statusLabel.setText("Status: Done 6");
                    System.out.println("done 6");
                    x6 = (int) getAverage(xVals);
                    y6 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 7:
                if (!isCal7) {
                    state = 0;
                    xVals = new int[15];
                    yVals = new int[15];
                    count = 0;


                } else if (count == 15) {
                    finished7 = true;
                    statusLabel.setText("Status: Done 7");
                    System.out.println("done 7");
                    x7 = (int) getAverage(xVals);
                    y7 = (int) getAverage(yVals);
                    xVals = new int[15];
                    yVals = new int[15];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 8: //finished calibrating
                CalibrateTouch ct = new CalibrateTouch();


                double[] rt = ct.get8Matrix7(x1, y1, x2, y2, x3, y3, x4, y4,
                        x5, y5, x6, y6, x7, y7,
                        100, 250, 400, 150, 700, 150,
                        1000, 250, 200, 600, 500, 500, 800, 600);

                System.out.println("100,250=" + x1 + "," + y1);
                System.out.println("400,150=" + x2 + "," + y2);
                System.out.println("700,150=" + x3 + "," + y3);
                System.out.println("1000,250=" + x4 + "," + y4);
                System.out.println("200,600=" + x5 + "," + y5);
                System.out.println("500,500=" + x6 + "," + y6);
                System.out.println("800,600=" + x7 + "," + y7);

                a = rt[0];
                b = rt[1];
                c = rt[2];
                d = rt[3];
                e = rt[4];
                f = rt[5];
                g = rt[6];
                h = rt[7];

                System.out.println("x=" + a + "x^2+" + b + "x+" + c + "y+" + d + "");
                System.out.println("x=" + e + "x+" + f + "y^2+" + g + "y+" + h + "");

                isCalibrating = false;
                findingLimit = false;
                getSettings().setA(a);
                getSettings().setB(b);
                getSettings().setC(c);
                getSettings().setD(d);
                getSettings().setE(e);
                getSettings().setF(f);
                saveImage.flush();
                saveImage = new BufferedImage(imWidth, imHeight, BufferedImage.TYPE_INT_ARGB);


                break;


        }

        if (finished1) {
            grap.setColor(Color.GREEN);


        } else if (isCal1) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(100, 250, 10, 10);
        grap.drawOval(85, 235, 40, 40);
        grap.drawLine(85, 255, 125, 255);
        grap.drawLine(105, 235, 105, 275);



        if (finished2) {
            grap.setColor(Color.GREEN);



        } else if (isCal2) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(400, 150, 10, 10);
        grap.drawOval(385, 135, 40, 40);
        grap.drawLine(385, 155, 425, 155);
        grap.drawLine(405, 135, 405, 175);



        if (finished3) {
            grap.setColor(Color.GREEN);



        } else if (isCal3) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(700, 150, 10, 10);
        grap.drawOval(685, 135, 40, 40);
        grap.drawLine(685, 155, 625, 155);
        grap.drawLine(705, 135, 705, 175);



        if (finished4) {
            grap.setColor(Color.GREEN);



        } else if (isCal4) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(1000, 250, 10, 10);
        grap.drawOval(985, 235, 40, 40);
        grap.drawLine(985, 255, 1025, 255);
        grap.drawLine(1005, 235, 1005, 275);



        if (finished5) {
            grap.setColor(Color.GREEN);



        } else if (isCal5) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(200, 600, 10, 10);
        grap.drawOval(185, 585, 40, 40);
        grap.drawLine(185, 605, 225, 605);
        grap.drawLine(205, 585, 205, 625);



        if (finished6) {
            grap.setColor(Color.GREEN);


        } else if (isCal6) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(500, 500, 10, 10);
        grap.drawOval(485, 485, 40, 40);
        grap.drawLine(485, 505, 525, 505);
        grap.drawLine(505, 485, 505, 525);



        if (finished7) {
            grap.setColor(Color.GREEN);


        } else if (isCal7) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(800, 600, 10, 10);
        grap.drawOval(785, 585, 40, 40);
        grap.drawLine(785, 605, 825, 605);
        grap.drawLine(805, 585, 805, 625);


        drawing.setImage(saveImage);
        drawLabel.setIcon(drawing);
        drawLabel.repaint();


    }

    public void calibrateSeven3(int valX, int valY) {

        isCal1 = cal.point1(valX, valY);
        isCal2 = cal.point2(valX, valY);
        isCal3 = cal.point3(valX, valY);
        isCal4 = cal.point4(valX, valY);
        isCal5 = cal.point5(valX, valY);
        isCal6 = cal.point6(valX, valY);
        isCal7 = cal.point7(valX, valY);

        switch (state) {
            case 0:
                if (finished1 && finished2 && finished3 && finished4 && finished5 && finished6 && finished7) {
                    state = 8;
                } else if (isCal1 && !finished1) {
                    count = 0;
                    state = 1;
                    System.out.println("State 1");
                } else if (isCal2 && !finished2) {
                    count = 0;
                    state = 2;
                    System.out.println("State 2");
                } else if (isCal3 && !finished3) {
                    count = 0;
                    state = 3;
                    System.out.println("State 3");
                } else if (isCal4 && !finished4) {
                    count = 0;
                    state = 4;
                    System.out.println("State 4");
                } else if (isCal5 && !finished5) {
                    count = 0;
                    state = 5;
                    System.out.println("State 5");
                } else if (isCal6 && !finished6) {
                    count = 0;
                    state = 6;
                    System.out.println("State 6");
                } else if (isCal7 && !finished7) {
                    count = 0;
                    state = 7;
                    System.out.println("State 7");
                }
                break;
            case 1:
                if (!isCal1 && count >= 30 && !isStable(xVals, yVals)) {
                    state = 0;
                    xVals = new int[30];
                    yVals = new int[30];
                    count = 0;


                } else if (count == 30) {
                    finished1 = true;
                    statusLabel.setText("Status: Done 1");
                    pm.playSound();
                    x1 = (int) getAverage(xVals);
                    y1 = (int) getAverage(yVals);
                    xVals = new int[30];
                    yVals = new int[30];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 2:
                if (!isCal2 && count >= 30 && !isStable(xVals, yVals)) {
                    state = 0;
                    xVals = new int[30];
                    yVals = new int[30];
                    count = 0;


                } else if (count == 30) {
                    finished2 = true;
                    statusLabel.setText("Status: Done 2");
                    pm.playSound();
                    x2 = (int) getAverage(xVals);
                    y2 = (int) getAverage(yVals);
                    xVals = new int[30];
                    yVals = new int[30];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 3:
                if (!isCal3 && count >= 30 && !isStable(xVals, yVals)) {
                    state = 0;
                    xVals = new int[30];
                    yVals = new int[30];
                    count = 0;


                } else if (count == 30) {
                    finished3 = true;
                    statusLabel.setText("Status: Done 3");
                    pm.playSound();
                    x3 = (int) getAverage(xVals);
                    y3 = (int) getAverage(yVals);
                    xVals = new int[30];
                    yVals = new int[30];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 4:
                if (!isCal4 && count >= 30 && !isStable(xVals, yVals)) {
                    state = 0;
                    xVals = new int[30];
                    yVals = new int[30];
                    count = 0;


                } else if (count == 30) {
                    finished4 = true;
                    statusLabel.setText("Status: Done 4");
                    pm.playSound();
                    x4 = (int) getAverage(xVals);
                    y4 = (int) getAverage(yVals);
                    state = 0;
                    xVals = new int[30];
                    yVals = new int[30];
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 5:
                if (!isCal5 && count >= 30 && !isStable(xVals, yVals)) {
                    state = 0;
                    xVals = new int[30];
                    yVals = new int[30];
                    count = 0;


                } else if (count == 30) {
                    finished5 = true;
                    statusLabel.setText("Status: Done 5");
                    pm.playSound();
                    x5 = (int) getAverage(xVals);
                    y5 = (int) getAverage(yVals);
                    xVals = new int[30];
                    yVals = new int[30];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 6:
                if (!isCal6 && count >= 30 && !isStable(xVals, yVals)) {
                    state = 0;
                    xVals = new int[30];
                    yVals = new int[30];
                    count = 0;


                } else if (count == 30) {
                    finished6 = true;
                    statusLabel.setText("Status: Done 6");
                    pm.playSound();
                    x6 = (int) getAverage(xVals);
                    y6 = (int) getAverage(yVals);
                    xVals = new int[30];
                    yVals = new int[30];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 7:
                if (!isCal7 && count >= 30 && !isStable(xVals, yVals)) {
                    state = 0;
                    xVals = new int[30];
                    yVals = new int[30];
                    count = 0;


                } else if (count == 30) {
                    finished7 = true;
                    statusLabel.setText("Status: Done 7");
                    pm.playSound();
                    x7 = (int) getAverage(xVals);
                    y7 = (int) getAverage(yVals);
                    xVals = new int[30];
                    yVals = new int[30];
                    state = 0;
                    count = 0;


                } else {
                    xVals[count] = valX;
                    yVals[count] = valY;
                    count++;

                }


                break;


            case 8: //finished calibrating
                CalibrateTouch ct = new CalibrateTouch();


                double[] rt = ct.get10Matrix7(x1, y1, x2, y2, x3, y3, x4, y4,
                        x5, y5, x6, y6, x7, y7,
                        100, 250, 400, 150, 700, 150,
                        1000, 250, 200, 600, 500, 500, 800, 600);

                System.out.println("100,250=" + x1 + "," + y1);
                System.out.println("400,150=" + x2 + "," + y2);
                System.out.println("700,150=" + x3 + "," + y3);
                System.out.println("1000,250=" + x4 + "," + y4);
                System.out.println("200,600=" + x5 + "," + y5);
                System.out.println("500,500=" + x6 + "," + y6);
                System.out.println("800,600=" + x7 + "," + y7);

                a = rt[0];
                b = rt[1];
                c = rt[2];
                d = rt[3];
                e = rt[4];
                f = rt[5];
                g = rt[6];
                h = rt[7];
                i = rt[8];
                j = rt[9];

                System.out.println("x=" + a + "x^2+" + b + "y^2+" + c + "x+" + d + "y+" + e);
                System.out.println("y=" + f + "x^2+" + g + "y^2+" + h + "x+" + i + "y+" + j);

                isCalibrating = false;
                findingLimit = false;
                getSettings().setA(a);
                getSettings().setB(b);
                getSettings().setC(c);
                getSettings().setD(d);
                getSettings().setE(e);
                getSettings().setF(f);
                getSettings().setG(g);
                getSettings().setH(h);
                getSettings().setI(i);
                getSettings().setJ(j);
                saveImage.flush();
                saveImage = new BufferedImage(imWidth, imHeight, BufferedImage.TYPE_INT_ARGB);
                break;
        }

        if (finished1) {
            grap.setColor(Color.GREEN);
        } else if (isCal1) {
            grap.setColor(Color.RED);
        } else {
            grap.setColor(Color.BLACK);
        }
        grap.fillOval(100, 250, 10, 10);
        grap.drawOval(85, 235, 40, 40);
        grap.drawLine(85, 255, 125, 255);
        grap.drawLine(105, 235, 105, 275);



        if (finished2) {
            grap.setColor(Color.GREEN);



        } else if (isCal2) {
            grap.setColor(Color.RED);
        } else {
            grap.setColor(Color.BLACK);
        }
        grap.fillOval(400, 150, 10, 10);
        grap.drawOval(385, 135, 40, 40);
        grap.drawLine(385, 155, 425, 155);
        grap.drawLine(405, 135, 405, 175);

        if (finished3) {
            grap.setColor(Color.GREEN);
        } else if (isCal3) {
            grap.setColor(Color.RED);
        } else {
            grap.setColor(Color.BLACK);
        }
        grap.fillOval(700, 150, 10, 10);
        grap.drawOval(685, 135, 40, 40);
        grap.drawLine(685, 155, 725, 155);
        grap.drawLine(705, 135, 705, 175);

        if (finished4) {
            grap.setColor(Color.GREEN);

        } else if (isCal4) {
            grap.setColor(Color.RED);

        } else {
            grap.setColor(Color.BLACK);
        }
        grap.fillOval(1000, 250, 10, 10);
        grap.drawOval(985, 235, 40, 40);
        grap.drawLine(985, 255, 1025, 255);
        grap.drawLine(1005, 235, 1005, 275);



        if (finished5) {
            grap.setColor(Color.GREEN);
        } else if (isCal5) {
            grap.setColor(Color.RED);
        } else {
            grap.setColor(Color.BLACK);
        }
        grap.fillOval(200, 600, 10, 10);
        grap.drawOval(185, 585, 40, 40);
        grap.drawLine(185, 605, 225, 605);
        grap.drawLine(205, 585, 205, 625);

        if (finished6) {
            grap.setColor(Color.GREEN);


        } else if (isCal6) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(500, 500, 10, 10);
        grap.drawOval(485, 485, 40, 40);
        grap.drawLine(485, 505, 525, 505);
        grap.drawLine(505, 485, 505, 525);



        if (finished7) {
            grap.setColor(Color.GREEN);


        } else if (isCal7) {
            grap.setColor(Color.RED);


        } else {
            grap.setColor(Color.BLACK);


        }
        grap.fillOval(800, 600, 10, 10);
        grap.drawOval(785, 585, 40, 40);
        grap.drawLine(785, 605, 825, 605);
        grap.drawLine(805, 585, 805, 625);


        drawing.setImage(saveImage);
        drawLabel.setIcon(drawing);
        drawLabel.repaint();


    }

    public void buttonPressingOld(int[] cordinate) {
        /*int x = cordinate[0];
        int y = cordinate[1];
        x = x + x_offset;
        y = y + y_offset;
        boolean onStart = x > startbuttonco[0] && (startbuttonco[0] + startButton.getWidth()) > x && y > startbuttonco[1] && (startbuttonco[1] + startButton.getHeight()) > y;
        boolean onStop = x > stopbuttonco[0] && (stopbuttonco[0] + stopButton.getWidth()) > x && y > stopbuttonco[1] && (stopbuttonco[1] + stopButton.getHeight()) > y;
        boolean onPause = x > pausebuttonco[0] && (pausebuttonco[0] + pauseButton.getWidth()) > x && y > pausebuttonco[1] && (pausebuttonco[1] + pauseButton.getHeight()) > y;
        boolean onCamera = x > camerabuttonco[0] && (camerabuttonco[0] + cameraButton.getWidth()) > x && y > camerabuttonco[1] && (camerabuttonco[1] + cameraButton.getHeight()) > y;
        boolean onRed = x > redbuttonco[0] && (redbuttonco[0] + redPenButton.getWidth()) > x && y > redbuttonco[1] && (redbuttonco[1] + redPenButton.getHeight()) > y;
        boolean onBlue = x > bluebuttonco[0] && (bluebuttonco[0] + bluePenButton.getWidth()) > x && y > bluebuttonco[1] && (bluebuttonco[1] + bluePenButton.getHeight()) > y;
        boolean onEraser = x > eraserbutonco[0] && (eraserbutonco[0] + eraserButton.getWidth()) > x && y > eraserbutonco[1] && (eraserbutonco[1] + eraserButton.getHeight()) > y;
        boolean onClear = x > clearbuttonco[0] && (clearbuttonco[0] + clearScreen.getWidth()) > x && y > clearbuttonco[1] && (clearbuttonco[1] + clearScreen.getHeight()) > y;
        boolean onImage = x > picbuttonco[0] && (picbuttonco[0] + imageButton.getWidth()) > x && y > picbuttonco[1] && (picbuttonco[1] + imageButton.getHeight()) > y;
        boolean onNext = x > nextbuttonco[0] && (nextbuttonco[0] + nextButton.getWidth()) > x && y > nextbuttonco[1] && (nextbuttonco[1] + nextButton.getHeight()) > y;
        boolean onPrev = x > backbuttonco[0] && (backbuttonco[0] + prevButton.getWidth()) > x && y > backbuttonco[1] && (backbuttonco[1] + prevButton.getHeight()) > y;
        boolean onBlack = x > blackbuttonco[0] && (blackbuttonco[0] + blackPenButton.getWidth()) > x && y > blackbuttonco[1] && (blackbuttonco[1] + blackPenButton.getHeight()) > y;
        
        switch (Bstate) {
        case 0:
        if (onStart) {
        Bstate = 1;
        } else if (onPause) {
        Bstate = 2;
        } else if (onStop) {
        Bstate = 3;
        } else if (onCamera) {
        Bstate = 4;
        } else if (onBlack) {
        Bstate = 5;
        } else if (onRed) {
        Bstate = 6;
        } else if (onBlue) {
        Bstate = 7;
        } else if (onEraser) {
        Bstate = 8;
        } else if (onClear) {
        Bstate = 9;
        } else if (onImage) {
        Bstate = 10;
        } else if (onNext) {
        Bstate = 11;
        } else if (onPrev) {
        Bstate = 12;
        }
        prevBstate = 0;
        break;
        case 1:
        if (onStart) {
        if (prevBstate == 1) {
        if (Bcount < 20) {
        Bcount++;
        Bstate = 1;
        } else if (Bcount == 20) {
        startButton.doClick();
        Bcount = 0;
        Bstate = 0;
        }
        } else {
        Bcount = 1;
        Bstate = 1;
        }
        } else {
        Bcount = 0;
        Bstate = 0;
        }
        prevBstate = 1;
        break;
        case 2:
        if (onPause) {
        if (prevBstate == 2) {
        if (Bcount < 20) {
        Bcount++;
        Bstate = 2;
        } else if (Bcount == 20) {
        pauseButton.doClick();
        Bcount = 0;
        Bstate = 0;
        }
        } else {
        Bcount = 1;
        Bstate = 2;
        }
        } else {
        Bcount = 0;
        Bstate = 0;
        }
        prevBstate = 1;
        break;
        case 3:
        if (onStop) {
        if (prevBstate == 3) {
        if (Bcount < 20) {
        Bcount++;
        Bstate = 3;
        } else if (Bcount == 20) {
        stopButton.doClick();
        Bcount = 0;
        Bstate = 0;
        }
        } else {
        Bcount = 1;
        Bstate = 3;
        }
        } else {
        Bcount = 0;
        Bstate = 0;
        }
        prevBstate = 3;
        break;
        
        case 4:
        if (onCamera) {
        if (prevBstate == 4) {
        if (Bcount < 20) {
        Bcount++;
        Bstate = 4;
        } else if (Bcount == 20) {
        cameraButton.doClick();
        Bcount = 0;
        Bstate = 0;
        }
        } else {
        Bcount = 1;
        Bstate = 4;
        }
        } else {
        Bcount = 0;
        Bstate = 0;
        }
        prevBstate = 4;
        break;
        
        case 5:
        if (onBlack) {
        if (prevBstate == 5) {
        if (Bcount < 20) {
        Bcount++;
        Bstate = 5;
        } else if (Bcount == 20) {
        blackPenButton.doClick();
        Bcount = 0;
        Bstate = 0;
        }
        } else {
        Bcount = 1;
        Bstate = 5;
        }
        } else {
        Bcount = 0;
        Bstate = 0;
        }
        prevBstate = 5;
        break;
        
        case 6:
        if (onRed) {
        if (prevBstate == 6) {
        if (Bcount < 20) {
        Bcount++;
        Bstate = 6;
        } else if (Bcount == 20) {
        redPenButton.doClick();
        Bcount = 0;
        Bstate = 0;
        }
        } else {
        Bcount = 1;
        Bstate = 6;
        }
        } else {
        Bcount = 0;
        Bstate = 0;
        }
        prevBstate = 6;
        break;
        
        case 7:
        if (onBlue) {
        if (prevBstate == 7) {
        if (Bcount < 20) {
        Bcount++;
        Bstate = 7;
        } else if (Bcount == 20) {
        bluePenButton.doClick();
        Bcount = 0;
        Bstate = 0;
        }
        } else {
        Bcount = 1;
        Bstate = 7;
        }
        } else {
        Bcount = 0;
        Bstate = 0;
        }
        prevBstate = 7;
        break;
        
        
        case 8:
        if (onEraser) {
        if (prevBstate == 8) {
        if (Bcount < 20) {
        Bcount++;
        Bstate = 8;
        } else if (Bcount == 20) {
        eraserButton.doClick();
        Bcount = 0;
        Bstate = 0;
        }
        } else {
        Bcount = 1;
        Bstate = 8;
        }
        } else {
        Bcount = 0;
        Bstate = 0;
        }
        prevBstate = 8;
        break;
        
        case 9:
        if (onClear) {
        if (prevBstate == 9) {
        if (Bcount < 20) {
        Bcount++;
        Bstate = 9;
        } else if (Bcount == 20) {
        clearScreen.doClick();
        Bcount = 0;
        Bstate = 0;
        }
        } else {
        Bcount = 1;
        Bstate = 9;
        }
        } else {
        Bcount = 0;
        Bstate = 0;
        }
        prevBstate = 9;
        break;
        
        
        case 10:
        if (onImage) {
        if (prevBstate == 10) {
        if (Bcount < 20) {
        Bcount++;
        Bstate = 10;
        } else if (Bcount == 20) {
        imageButton.doClick();
        Bcount = 0;
        Bstate = 0;
        }
        } else {
        Bcount = 1;
        Bstate = 10;
        }
        } else {
        Bcount = 0;
        Bstate = 0;
        }
        prevBstate = 10;
        break;
        
        case 11:
        if (onNext) {
        if (prevBstate == 11) {
        if (Bcount < 20) {
        Bcount++;
        Bstate = 11;
        } else if (Bcount == 20) {
        nextButton.doClick();
        Bcount = 0;
        Bstate = 0;
        }
        } else {
        Bcount = 1;
        Bstate = 11;
        }
        } else {
        Bcount = 0;
        Bstate = 0;
        }
        prevBstate = 11;
        break;
        
        case 12:
        if (onPrev) {
        if (prevBstate == 12) {
        if (Bcount < 20) {
        Bcount++;
        Bstate = 12;
        } else if (Bcount == 20) {
        prevButton.doClick();
        Bcount = 0;
        Bstate = 0;
        }
        } else {
        Bcount = 1;
        Bstate = 12;
        }
        } else {
        Bcount = 0;
        Bstate = 0;
        }
        prevBstate = 12;
        break;
        }
        // System.out.println("state:="+Bstate);
        statusLabel.setText("state:=" + Bstate);*/
    }

    public void buttonPressing(int[] cordinate) {
        int x = cordinate[0];
        int y = cordinate[1];
        boolean onBlack = x > minXBut && maxXBut > x && y > midBut[0] && midBut[1] > y;
        boolean onRed = x > minXBut && maxXBut > x && y > midBut[1] && midBut[2] > y;
        boolean onBlue = x > minXBut && maxXBut > x && y > midBut[2] && midBut[3] > y;
        boolean onEraser = x > minXBut && maxXBut > x && y > midBut[3] && midBut[4] > y;
        boolean onClear = x > minXBut && maxXBut > x && y > midBut[4] && midBut[5] > y;
        boolean onImage = x > minXBut && maxXBut > x && y > midBut[5] && midBut[6] > y;
        boolean onNext = x > minXBut && maxXBut > x && y > midBut[6] && midBut[7] > y;
        boolean onPrev = x > minXBut && maxXBut > x && y > midBut[7] && midBut[8] > y;
        switch (Bstate) {
            case 0:
                if (onBlack) {
                    Bstate = 1;

                } else if (onRed) {
                    Bstate = 2;

                } else if (onBlue) {
                    Bstate = 3;

                } else if (onEraser) {
                    Bstate = 4;

                } else if (onClear) {
                    Bstate = 5;

                } else if (onImage) {
                    Bstate = 6;

                } else if (onNext) {
                    Bstate = 7;

                } else if (onPrev) {
                    Bstate = 8;

                }
                prevBstate = 0;
                break;
            case 1:
                if (onBlack) {
                    if (prevBstate == 1) {
                        if (Bcount < 10) {
                            Bcount++;
                            Bstate = 1;
                        } else if (Bcount == 10) {
                            pm.playSound();
                            blackPenButton.doClick();
                            Bcount = 0;
                            Bstate = 0;
                        }
                    } else {
                        Bcount = 1;
                        Bstate = 1;
                    }
                } else {
                    Bcount = 0;
                    Bstate = 0;
                }
                prevBstate = 1;
                break;
            case 2:
                if (onRed) {
                    if (prevBstate == 2) {
                        if (Bcount < 10) {
                            Bcount++;
                            Bstate = 2;
                        } else if (Bcount == 10) {
                            pm.playSound();
                            redPenButton.doClick();
                            Bcount = 0;
                            Bstate = 0;
                        }
                    } else {
                        Bcount = 1;
                        Bstate = 2;
                    }
                } else {
                    Bcount = 0;
                    Bstate = 0;
                }
                prevBstate = 2;
                break;
            case 3:
                if (onBlue) {
                    if (prevBstate == 3) {
                        if (Bcount < 10) {
                            Bcount++;
                            Bstate = 3;
                        } else if (Bcount == 10) {
                            pm.playSound();
                            bluePenButton.doClick();
                            Bcount = 0;
                            Bstate = 0;
                        }
                    } else {
                        Bcount = 1;
                        Bstate = 3;
                    }
                } else {
                    Bcount = 0;
                    Bstate = 0;
                }
                prevBstate = 3;
                break;
            case 4:
                if (onEraser) {
                    if (prevBstate == 4) {
                        if (Bcount < 10) {
                            Bcount++;
                            Bstate = 4;
                        } else if (Bcount == 10) {
                            pm.playSound();
                            eraserButton.doClick();
                            Bcount = 0;
                            Bstate = 0;
                        }
                    } else {
                        Bcount = 1;
                        Bstate = 4;
                    }
                } else {
                    Bcount = 0;
                    Bstate = 0;
                }
                prevBstate = 4;
                break;
            case 5:
                if (onClear) {
                    if (prevBstate == 5) {
                        if (Bcount < 15) {
                            Bcount++;
                            Bstate = 5;
                        } else if (Bcount == 15) {
                            pm.playSound();
                            clearScreen.doClick();
                            Bcount = 0;
                            Bstate = 0;
                        }
                    } else {
                        Bcount = 1;
                        Bstate = 5;
                    }
                } else {
                    Bcount = 0;
                    Bstate = 0;
                }
                prevBstate = 5;
                break;
            case 6:
                if (onImage) {
                    if (prevBstate == 6) {
                        if (Bcount < 15) {
                            Bcount++;
                            Bstate = 6;
                        } else if (Bcount == 15) {
                            pm.playSound();
                            saveButton.doClick();
                            Bcount = 0;
                            Bstate = 0;
                        }
                    } else {
                        Bcount = 1;
                        Bstate = 6;
                    }
                } else {
                    Bcount = 0;
                    Bstate = 0;
                }
                prevBstate = 6;
                break;
            case 7:
                if (onNext) {
                    if (prevBstate == 7) {
                        if (Bcount < 12) {
                            Bcount++;
                            Bstate = 7;
                        } else if (Bcount == 12) {
                            pm.playSound();
                            nextButton.doClick();
                            Bcount = 0;
                            Bstate = 0;
                        }
                    } else {
                        Bcount = 1;
                        Bstate = 7;
                    }
                } else {
                    Bcount = 0;
                    Bstate = 0;
                }
                prevBstate = 7;
                break;
            case 8:
                if (onPrev) {
                    if (prevBstate == 8) {
                        if (Bcount < 12) {
                            Bcount++;
                            Bstate = 8;
                        } else if (Bcount == 12) {
                            pm.playSound();
                            prevButton.doClick();
                            Bcount = 0;
                            Bstate = 0;
                        }
                    } else {
                        Bcount = 1;
                        Bstate = 8;
                    }
                } else {
                    Bcount = 0;
                    Bstate = 0;
                }
                prevBstate = 8;
                break;
        } // System.out.println("state:="+Bstate);
        //  statusLabel.setText("State : = " + Bstate);
        setButtonStatus(Bstate);
    }

    public void locationMapper() {

        drawlabelco[0] = drawLabel.getLocationOnScreen().x + 30;
        drawlabelco[

1] = drawLabel.getLocationOnScreen().y - 30;

        jmenuco[

0] = jMenuBar1.getLocationOnScreen().x;
        jmenuco[

1] = jMenuBar1.getLocationOnScreen().y;

        startbuttonco[

0] = startButton.getLocationOnScreen().x;
        startbuttonco[

1] = startButton.getLocationOnScreen().y;

        pausebuttonco[

0] = pauseButton.getLocationOnScreen().x;
        pausebuttonco[

1] = pauseButton.getLocationOnScreen().y;

        stopbuttonco[

0] = stopButton.getLocationOnScreen().x;
        stopbuttonco[

1] = stopButton.getLocationOnScreen().y;

        camerabuttonco[

0] = cameraButton.getLocationOnScreen().x;
        camerabuttonco[

1] = cameraButton.getLocationOnScreen().y;

        redbuttonco[

0] = redPenButton.getLocationOnScreen().x;
        redbuttonco[

1] = redPenButton.getLocationOnScreen().y;

        bluebuttonco[

0] = bluePenButton.getLocationOnScreen().x;
        bluebuttonco[

1] = bluePenButton.getLocationOnScreen().y;

        eraserbutonco[

0] = eraserButton.getLocationOnScreen().x;
        eraserbutonco[

0] = eraserButton.getLocationOnScreen().y;

        clearbuttonco[

0] = clearScreen.getLocationOnScreen().x;
        clearbuttonco[

1] = clearScreen.getLocationOnScreen().y;

        picbuttonco[

0] = saveButton.getLocationOnScreen().x;
        picbuttonco[

1] = saveButton.getLocationOnScreen().y;

        nextbuttonco[

0] = nextButton.getLocationOnScreen().x;
        nextbuttonco[

1] = nextButton.getLocationOnScreen().y;

        backbuttonco[

0] = prevButton.getLocationOnScreen().x;
        backbuttonco[

1] = prevButton.getLocationOnScreen().y;

        x_offset = drawlabelco[0];
        y_offset = drawlabelco[1];


//        System.out.println("x=" + bluebuttonco[0] + " y=" + bluebuttonco[1]);
//        statusLabel.setText("x=" + bluebuttonco[0] + " y=" + bluebuttonco[1]);


    }
    int[] buttonMidX = new int[9];
    int[] buttonMidY = new int[9];
    int buttonOn = 0;
    boolean calibrateEach = false;
    int butNo;
    String butName;

    public boolean stableRange(int[] arX, int[] arY) {
        Arrays.sort(arX);
        Arrays.sort(arY);


        int kot = arX[20] - arX[11];


        int uol = arY[20] - arY[11];



        if (kot > 15 || uol > 15) {
            return false;


        } else {
            return true;


        }
    }

    public void calibrateButton(int valX, int valY) {
        switch (buttonOn) {
            case 0:
                statusLabel.setText("Status: Touch the" + butName + "  Button");


                if (buttonRange(valX, valY)) {
                    buttonOn = 1;


                }
                break;


            case 1:
                statusLabel.setText("Status: Touching the" + butName + "  Button");


                if (buttonRange(valX, valY)) {
                    buttonOn = 1;
                    butX[butCount] = valX;
                    butY[butCount] = valY;
                    butCount++;

                    if (butCount == 30) {
                        if (stableRange(butX, butY)) {
                            buttonMidX[butNo] = (int) getButAverage(butX);
                            buttonMidY[butNo] = (int) getButAverage(butY);
                            buttonOn = 0;
                            butX = new int[30];
                            butY = new int[30];
                            //minimumY = buttonMidY[1];
                            butCount = 0;
                            calibrateEach = false;
                            statusLabel.setText("Status: Calibrated " + butName + "  Button");
                            System.out.println("Button " + butNo + "=" + buttonMidX[butNo] + "," + buttonMidY[butNo]);


                        } else {
                            buttonOn = 0;
                            butX = new int[30];
                            butY = new int[30];
                            butCount = 0;


                        }
                    }
                } else {
                    buttonOn = 0;
                    butX = new int[30];
                    butY = new int[30];
                    butCount = 0;


                }
                break;


        }
    }

    public void buttonCalibration(int valX, int valY) {
        switch (buttonStatus) {
            case 0:
                statusLabel.setText("Status: Touch the 1st Button");


                if (buttonRange(valX, valY)) {
                    buttonStatus = 1;


                }
                break;


            case 1:
                statusLabel.setText("Status: Touching the 1st Button");


                if (buttonRange(valX, valY)) {
                    buttonStatus = 1;
                    butX[butCount] = valX;
                    butY[butCount] = valY;
                    butCount++;

                    if (butCount == 30) {
                        buttonMidX[1] = (int) getButAverage(butX);
                        buttonMidY[

1] = (int) getButAverage(butY);
                        buttonStatus = 2;
                        butX = new int[30];
                        butY = new int[30];
                        minimumY = buttonMidY[1];
                        butCount = 0;
                        System.out.println("Button 1=" + buttonMidX[1] + "," + buttonMidY[1]);


                    }
                } else {
                    buttonStatus = 0;
                    butX = new int[30];
                    butY = new int[30];
                    butCount = 0;


                }
                break;


            case 2:
                statusLabel.setText("Status: Touch the 2nd Button");


                if (buttonRange(valX, valY)) {
                    buttonStatus = 3;


                }
                break;


            case 3:
                statusLabel.setText("Status: Touching the 2nd Button");


                if (buttonRange(valX, valY)) {
                    buttonStatus = 3;
                    butX[butCount] = valX;
                    butY[butCount] = valY;
                    butCount++;

                    if (butCount == 30) {
                        buttonMidX[2] = (int) getButAverage(butX);
                        buttonMidY[

2] = (int) getButAverage(butY);
                        buttonStatus = 4;
                        butX = new int[30];
                        butY = new int[30];
                        minimumY = buttonMidY[2];
                        butCount = 0;
                        System.out.println("Button 2=" + buttonMidX[2] + "," + buttonMidY[2]);


                    }
                } else {
                    buttonStatus = 2;
                    butX = new int[30];
                    butY = new int[30];
                    butCount = 0;


                }
                break;


            case 4:
                statusLabel.setText("Status: Touch the 3rd Button");


                if (buttonRange(valX, valY)) {
                    buttonStatus = 5;


                }
                break;


            case 5:
                statusLabel.setText("Status: Touching the 3rd Button");


                if (buttonRange(valX, valY)) {
                    buttonStatus = 5;
                    butX[butCount] = valX;
                    butY[butCount] = valY;
                    butCount++;

                    if (butCount == 30) {
                        buttonMidX[3] = (int) getButAverage(butX);
                        buttonMidY[

3] = (int) getButAverage(butY);
                        buttonStatus = 6;
                        butX = new int[30];
                        butY = new int[30];
                        minimumY = buttonMidY[3];
                        butCount = 0;
                        System.out.println("Button 3=" + buttonMidX[3] + "," + buttonMidY[3]);


                    }
                } else {
                    buttonStatus = 4;
                    butX = new int[30];
                    butY = new int[30];
                    butCount = 0;


                }
                break;


            case 6:
                statusLabel.setText("Status: Touch the 4th Button");


                if (buttonRange(valX, valY)) {
                    buttonStatus = 7;


                }
                break;


            case 7:
                statusLabel.setText("Status: Touching the 4th Button");

                if (buttonRange(valX, valY)) {
                    buttonStatus = 7;
                    butX[butCount] = valX;
                    butY[butCount] = valY;
                    butCount++;

                    if (butCount == 30) {
                        buttonMidX[4] = (int) getButAverage(butX);
                        buttonMidY[

4] = (int) getButAverage(butY);
                        buttonStatus = 8;
                        butX = new int[30];
                        butY = new int[30];
                        minimumY = buttonMidY[4];
                        butCount = 0;
                        System.out.println("Button 4=" + buttonMidX[4] + "," + buttonMidY[4]);


                    }
                } else {
                    buttonStatus = 6;
                    butX = new int[30];
                    butY = new int[30];
                    butCount = 0;


                }
                break;


            case 8:
                statusLabel.setText("Status: Touch the 5th Button");


                if (buttonRange(valX, valY)) {
                    buttonStatus = 9;


                }
                break;


            case 9:
                statusLabel.setText("Status: Touching the 5th Button");


                if (buttonRange(valX, valY)) {
                    buttonStatus = 9;
                    butX[butCount] = valX;
                    butY[butCount] = valY;
                    butCount++;

                    if (butCount == 30) {
                        buttonMidX[5] = (int) getButAverage(butX);
                        buttonMidY[

5] = (int) getButAverage(butY);
                        buttonStatus = 10;
                        butX = new int[30];
                        butY = new int[30];
                        minimumY = buttonMidY[5];
                        butCount = 0;
                        System.out.println("Button 5=" + buttonMidX[5] + "," + buttonMidY[5]);


                    }
                } else {
                    buttonStatus = 8;
                    butX = new int[30];
                    butY = new int[30];
                    butCount = 0;


                }
                break;


            case 10:
                statusLabel.setText("Status: Touch the 6th Button");


                if (buttonRange(valX, valY)) {
                    buttonStatus = 11;


                }
                break;


            case 11:
                statusLabel.setText("Status: Touching the 6th Button");


                if (buttonRange(valX, valY)) {
                    buttonStatus = 11;
                    butX[butCount] = valX;
                    butY[butCount] = valY;
                    butCount++;

                    if (butCount == 30) {
                        buttonMidX[6] = (int) getButAverage(butX);
                        buttonMidY[

6] = (int) getButAverage(butY);
                        buttonStatus = 12;
                        butX = new int[30];
                        butY = new int[30];
                        minimumY = buttonMidY[6];
                        butCount = 0;
                        System.out.println("Button 6=" + buttonMidX[6] + "," + buttonMidY[6]);


                    }
                } else {
                    buttonStatus = 10;
                    butX = new int[30];
                    butY = new int[30];
                    butCount = 0;


                }
                break;


            case 12:
                statusLabel.setText("Status: Touch the 7th Button");


                if (buttonRange(valX, valY)) {
                    buttonStatus = 13;


                }
                break;


            case 13:
                statusLabel.setText("Status: Touching the 7th Button");


                if (buttonRange(valX, valY)) {
                    buttonStatus = 13;
                    butX[butCount] = valX;
                    butY[butCount] = valY;
                    butCount++;

                    if (butCount == 30) {
                        buttonMidX[7] = (int) getButAverage(butX);
                        buttonMidY[

7] = (int) getButAverage(butY);
                        buttonStatus = 14;
                        butX = new int[30];
                        butY = new int[30];
                        minimumY = buttonMidY[7];
                        butCount = 0;
                        System.out.println("Button 7=" + buttonMidX[7] + "," + buttonMidY[7]);


                    }
                } else {
                    buttonStatus = 12;
                    butX = new int[30];
                    butY = new int[30];
                    butCount = 0;


                }
                break;


            case 14:
                statusLabel.setText("Status: Touch the 8th Button");


                if (buttonRange(valX, valY)) {
                    buttonStatus = 15;


                }
                break;


            case 15:
                statusLabel.setText("Status: Touching the 8th Button");


                if (buttonRange(valX, valY)) {
                    buttonStatus = 15;
                    butX[butCount] = valX;
                    butY[butCount] = valY;
                    butCount++;

                    if (butCount == 30) {
                        buttonMidX[8] = (int) getButAverage(butX);
                        buttonMidY[

8] = (int) getButAverage(butY);
                        buttonStatus = 16;
                        butX = new int[30];
                        butY = new int[30];
                        minimumY = buttonMidY[8];
                        butCount = 0;
                        System.out.println("Button 8=" + buttonMidX[8] + "," + buttonMidY[8]);


                    }
                } else {
                    buttonStatus = 14;
                    butX = new int[30];
                    butY = new int[30];
                    butCount = 0;


                }
                break;


            case 16:
                statusLabel.setText("Status: Calibration completed succesfully");


                for (int uyu : buttonMidX) {
                    System.out.print(uyu + " ");


                }
                System.out.println("");



                for (int uyu : buttonMidY) {
                    System.out.print(uyu + " ");


                }

                calMidBut();
                String path = JOptionPane.showInputDialog(rootPane, "Enter a name for the calibration file", "Calibration" + getDate());
                saveArray(
                        path);

                System.out.println("");
                calibrateButton = false;


                break;


        }
    }
    int minimumY = 200;

    public void saveArray(String name) {
        name = name + ".ser";


        try {
            FileOutputStream fileOut = new FileOutputStream(name);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            ButtonCalib jk = new ButtonCalib();
            jk.xVal = buttonMidX;
            jk.yVal = buttonMidY;
            out.writeObject(jk);
            out.close();
            fileOut.close();



        } catch (IOException ex) {
            Logger.getLogger(ProjectorGUINew.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    public void calMidBut() {

        midBut[0] = buttonMidY[1] - 39;
        midBut[

1] = (buttonMidY[2] + buttonMidY[1]) / 2;
        midBut[

2] = (buttonMidY[3] + buttonMidY[2]) / 2;
        midBut[

3] = (buttonMidY[4] + buttonMidY[3]) / 2;
        midBut[

4] = (buttonMidY[5] + buttonMidY[4]) / 2;
        midBut[

5] = (buttonMidY[6] + buttonMidY[5]) / 2;
        midBut[

6] = (buttonMidY[7] + buttonMidY[6]) / 2;
        midBut[

7] = (buttonMidY[8] + buttonMidY[7]) / 2;
        midBut[

8] = buttonMidY[8] + 39;



        int average = 0;


        for (int pllo = 1; pllo
                < 9; pllo++) {
            average = buttonMidX[pllo] + average;


        }
        minXBut = (int) average / 8 - 30;
        maxXBut = (int) average / 8 + 30;


    }

    public boolean buttonRange(int x, int y) {
        if (1200 < x && x < 1500 && minimumY < y && y < 900) {
            return true;


        } else {
            return false;


        }
    }

    private double getButAverage(int[] ab) {

        Arrays.sort(ab);


        double fg = 0;
//
//        for (int jkl : ab) {
//            System.out.print(jkl + " ");
//        }
//


        for (int hr = 11; hr
                <= 20; hr++) {
            fg = fg + ab[hr];
            System.out.print(ab[hr] + " ");


        }
        System.out.println("");


        return fg / 10;


    }

    private String getDate() {
        Date now = new Date();
        SimpleDateFormat df = new SimpleDateFormat("E yy.MM.dd '@'hh.mm");
        String name = df.format(now);


        return name;

    }

    private void setButtonStatus(int bstat) {
        switch (bstat) {
            case 1:
                statusLabel.setText("State : = Black Pen");
                break;
            case 2:
                statusLabel.setText("State : = Red Pen");
                break;
            case 3:
                statusLabel.setText("State : = Blue Pen");
                break;
            case 4:
                statusLabel.setText("State : = Eraser");
                break;
            case 5:
                statusLabel.setText("State : = Clear");
                break;
            case 6:
                statusLabel.setText("State : = Next Page");
                break;
            case 7:
                statusLabel.setText("State : = Previous Page");
                break;
        }
    }
}
//
//             af = new ArrayFilter(filterSize);
//                            af2 = new ArrayFilter(filterSize);
//                            restor = true;
//                            shashiStatus = 2;

